SZU:B47 Big Integer I
Judge Info
- Memory Limit: 32768KB
- Case Time Limit: 10000MS
- Time Limit: 10000MS
- Judger: Normal
Description
Please calculate the answer of A+B and the answer of A-B, both A and B are integer.
Input
The first line of input contains
, the number of test cases. There is only line for each test case. It contains two integers
.
Output
For each test case, output two lines A+B and A-B.
Sample Input
3
1 2
1 1
-1 -1
Sample Output
3
-1
2
0
-2
0
题解:
1. 调试了很多遍,WA,之后朋友用python 测试很多大数据,我都测试了,数据正常,但还是WA,问了cxc,才发现,擦擦,尼玛,爆粗有木有? 调代码调了四个多小时只是没考虑前导0 ?带前导0也算神马整数 , -000001 是 -1 0000002 是 2 , 哪有这么无语的数据,擦擦!!
2. 第二次调试发现前导0去掉了。但是忽略了一个重要的数据,妹的 -00000 减去 +0000000 等于 -0 的有木有? 又单独考虑0的符号问题,最终AC。
虽然我代码写的很啰嗦,也很难懂,给自己做个留念吧。
#include <stdio.h>
#include <string.h> #define max(a, b) ((a) >= (b) ? (a) : (b)) char A[];
char B[];
int C[];
int mark; char *revstr(char *str, size_t len)
{ char *start = str;
char *end = str + len - ;
char ch; if (str != NULL)
{
while (start < end)
{
ch = *start;
*start++ = *end;
*end-- = ch;
}
}
return str;
} void add(char A[], char B[]){ int i, lenA, lenB, len;
memset(C,,sizeof(C)); lenA = strlen(A);
lenB = strlen(B);
len = max(lenA, lenB); revstr(A,lenA);
revstr(B,lenB); A[lenA] = '';
B[lenB] = ''; for(i=;i<len+;i++){ C[i] = (A[i] - '') + (B[i] - '') ;
} for(i=;i<len+;i++){
if(C[i] > ){
C[i]%=;
C[i+]++;
}
} if(mark == ) putchar('-');
if(C[len]==){
for(i=len-;i>=;--i)
printf("%d", C[i]);
printf("\n");
} else {
for(i=len;i>=;--i)
printf("%d", C[i]);
printf("\n");
}
A[lenA] = '\0';
B[lenB] = '\0';
revstr(A,lenA);
revstr(B,lenB); } void minus(char A[], char B[]){ int i, lenA, lenB, len;
memset(C,,sizeof(C)); lenA = strlen(A);
lenB = strlen(B);
len = max(lenA, lenB);
revstr(A,lenA);
revstr(B,lenB); A[lenA] = '';
B[lenB] = ''; for(i=;i<len;i++){ C[i] = (A[i] - '') - (B[i] - '') ;
} for(i=;i<len;++i){
if(C[i] < ){
C[i] += ;
C[i+]--;
}
} if(mark == ) putchar('-'); for(;C[len]==;--len)
if(len==)
break;
for(i=len;i>=;--i)
printf("%d", C[i]);
printf("\n"); A[lenA] = '\0';
B[lenB] = '\0';
revstr(A,lenA);
revstr(B,lenB);
} int main(int argc, char const *argv[])
{
int t, i, lenA, lenB, len, Amark, Bmark; scanf("%d", &t);
while(t--){ mark = ;
Amark = ;
Bmark = ; memset(A,'',sizeof(A));
memset(B,'',sizeof(B)); scanf("%s", A);
scanf("%s", B); lenA = strlen(A);
lenB = strlen(B); int len;
len = max(lenA, lenB); if(A[] == '-'){
for(i=;i<lenA;++i)
A[i-]=A[i];
A[i-]='\0';
A[i] = '';
Amark = ;
} if(B[] == '-'){
for(i=;i<lenB;++i)
B[i-]=B[i];
B[i-]='\0';
B[i] = '';
Bmark = ;
} while(A[]== ''){
for(i=;i<lenA;++i)
A[i]=A[i+];
A[lenA]='';
} if(A[]=='\0'){
A[]='';
A[]='\0';
} while(B[]== ''){
for(i=;i<lenB;++i)
B[i]=B[i+];
B[lenB]=''; } if(B[]=='\0'){
B[]='';
B[]='\0';
} if(A[] == '' && B[] == ''){
printf("0\n");
printf("0\n");
continue;
} lenA = strlen(A);
lenB = strlen(B);
len = max(lenA, lenB); lenA = strlen(A);
lenB = strlen(B); if(Amark == && Bmark == ){
mark = ;
add(A,B); if(lenA>lenB){
mark = ;
minus(A,B);
}
else if(lenA < lenB){
mark = ;
minus(B,A);
}
else {
if(strcmp(A,B)>){
mark = ;
minus(A,B);
}
else if(strcmp(A,B)<){
mark = ;
minus(B,A);
}
else {mark = ;minus(A,B);}
}
continue;
} if(Amark == && Bmark ==){ mark = ;
add(A,B); if(lenA>lenB){
mark = ;
minus(A,B);
}
else if(lenA<lenB){
mark = ;
minus(B,A);
}
else {
if(strcmp(A,B)>){
mark = ;
minus(A,B);
}
else if(strcmp(A,B)<){
mark = ;
minus(B,A);
}
else{
minus(A,B);
}
}
continue;
} if(Amark == && Bmark == ){
if(lenA>lenB){
mark = ;
minus(A,B);
}
else if(lenA<lenB){
mark = ;
minus(B,A);
}
else {
if(strcmp(A,B)>){
mark = ;
minus(A,B);
}
else if(strcmp(A,B)<){
mark = ;
minus(B,A);
}
else{
mark = ;
minus(A,B);
}
} mark = ;
add(A,B);
continue;
} if(Amark == && Bmark == ){
if(lenA>lenB){
mark = ;
minus(A,B);
}
else if(lenA<lenB){
mark = ;
minus(B,A);
}
else{
if(strcmp(A,B)>){
mark = ;
minus(A,B);
}
else if(strcmp(A,B)<){
mark = ;
minus(B,A);
}
else {
mark = ;
minus(A,B);
}
}
mark = ;
add(A,B);
continue;
}
} return ;
}
SZU:B47 Big Integer I的更多相关文章
- SZU:B47 Big Integer II
Judge Info Memory Limit: 32768KB Case Time Limit: 10000MS Time Limit: 10000MS Judger: Normal Descrip ...
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- Integer.parseInt 引发的血案
Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...
- 由一个多线程共享Integer类变量问题引起的。。。
最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...
- [LeetCode] Integer Replacement 整数替换
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
随机推荐
- HBase加Solr
HBase加Solr 如何解决分布式系统数据事务一致性问题 (HBase加Solr) 摘要:对于所有的分布式系统,我想事务一致性问题是极其非常重要的问题,因为它直接影响到系统的可用性.本文以下所述所要 ...
- 深度-first遍历图--邻接表实现
在这里,邻接表的实现与深度优先遍历图,使用递归. #include<iostream> using namespace std; #define VERTEXNUM 5//结点数 stru ...
- 阅读INI档 - Delphi一片
程序往往需要读一些用户设置值.如何完成这一过程? B/S程序一般使用XML档.和C/S程序使用INI档. 前篇<C#迁移之callXBFLibrary - 2(调用非托管DLL)>是C#读 ...
- Oracle Global Finanicals Technical Reference(一个)
Skip Headers Oracle Global Finanicals Oracle Global Financials Technical Reference Manual Release 11 ...
- Mysql sql声明召回2 --正则表达式
这里简单回顾mysql在过滤后的数据使用正则表达式时.主要利用每方法将样品,或添加一些简短的说明. 下面说说提前,使用正则表达式是用来过滤数据 REGEXPkeyword 基本字符相匹配: SELEC ...
- JS里写入(混写)php asp
原文:JS里写入(混写)php asp JS里写入(混写)php 方法1:<Br> <script language="javascript"> docum ...
- 【IOS】 遍历info 所有内容 && 唯一的节能设备UUID
/**获取装置imie*/ std::string DeviceInfo::getIMIE() { #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) NSStri ...
- 小记NodeJS两项小技巧(与HTTP相关)
1,兼容HTTP1.1和HTTP1.0 区别在content-length,1.0接受定长,不接受变长,导致transfer-encodeing的chunked模式无法识别,最终导致无法识别BODY. ...
- JavaScript随记汇总
1.<script>标签嵌套,浏览器无法正常解析的问题: 百度知道回答 <script>FTAPI_slotid = 1007894;FTAPI_sync = true< ...
- 【视频】零基础学Android开发:蓝牙聊天室APP(二)
零基础学Android开发:蓝牙聊天室APP第二讲 2.1 课程内容应用场景 2.2 Android UI设计 2.3 组件布局:LinearLayout和RelativeLayout 2.4 Tex ...