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的更多相关文章

  1. SZU:B47 Big Integer II

    Judge Info Memory Limit: 32768KB Case Time Limit: 10000MS Time Limit: 10000MS Judger: Normal Descrip ...

  2. LeetCode 7. Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...

  3. Integer.parseInt 引发的血案

    Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...

  4. 由一个多线程共享Integer类变量问题引起的。。。

    最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...

  5. [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 ...

  6. [LeetCode] Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  7. [LeetCode] Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  8. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  9. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. java回调机制及其实现(转)

    1. 什么是回调函数 回调函数,顾名思义,用于回调的函数.回调函数只是一个功能片段,由用户按照回调函数调用约定来实现的一个函数.回调函数是一个工作流的一部分,由工作流来决定函数的调用(回调)时机.回调 ...

  2. AFNetWorking POST Multi-Part Request 上传图片

    这些天来,做图片上传的时候,我遇到一个问题.对我来说,这只是一个附加的图片将请求超时,这里是代码: AFHTTPRequestOperationManager *manager = [AFHTTPRe ...

  3. DFS-hdu-2821-Pusher

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2821 题目意思: 给一个n*n的矩阵,里面有些位置是空的,有些位置有箱子(a代表一个箱子,b代表两个 ...

  4. hdu1881 毕业bg(深搜索dfs)

    主题链接:pid=1881">http://acm.hdu.edu.cn/showproblem.php? pid=1881 ----------------------------- ...

  5. linux_mac_配置itrem2 rz sz_bug处理

    0:传输 .jar 等文件有问题 是  添加 sz -bye  以二进制流方式传输 1:安装  homebrew 2: brew install lrzsz 3:搜索 iterm2-recv-zmod ...

  6. HDU4405-Aeroplane chess(可能性DP需求预期)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. Java数据结构与算法(5) - ch05链表(LinkList)

    双端链表与传统链表非常相似,但是它有一个新增的特性:即对最后一个链节点的引用,就像对第一个连接点的引用一样.注意与双向链表进行区别.

  8. csu 1503: 点弧之间的距离-湖南省第十届大学生计算机程序设计大赛

    这是--比量p并用交点连接中心不上弧.在于:它至p距离.是不是p与端点之间的最短距离 #include<iostream> #include<map> #include< ...

  9. web实现QQ第三方登录

    开放平台-web实现QQ第三方登录   应用场景     web应用通过QQ登录授权实现第三方登录.   操作步骤     1  注册成为QQ互联平台开发者,http://connect.qq.com ...

  10. 刚学unity3d,跟着仿作了flappy bird,记下一些琐碎的心得!

    1.关于场景,即scene. 一个正常的游戏至少要有三个场景,即菜单(或者文件夹)场景.游戏关卡场景.游戏结束场景.它们一般统一放在project文件夹下scene文件夹(自己创建)中,方便管理. 1 ...