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. 移动web开发经验总结(1) (转)

    1.<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, minimum-sca ...

  2. java maven quartz exampe 实用指南

    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  3. Linux互斥和同步应用程序(一):posix线程和线程之间的相互排斥

    [版权声明:尊重原创,转载请保留出处:blog.csdn.net/shallnet 或 .../gentleliu,文章仅供学习交流.请勿用于商业用途] 有了进程的概念,为何还要使用线程呢? 首先,回 ...

  4. SSIS从理论到实战,再到应用(5)----流程控制之Foreach循环

    原文:SSIS从理论到实战,再到应用(5)----流程控制之Foreach循环 上期回顾: SSIS从理论到实战,再到应用(4)----流程控制之For循环 上一期讲了For循环,Foreach循环相 ...

  5. http headers

    什么是HTTP Headers HTTP是“Hypertext Transfer Protocol”的所写,整个万维网都在使用这种协议,几乎你在浏览器里看到的大部分内容都是通过http协议来传输的,比 ...

  6. EpPlus读取生成Excel帮助类+读取csv帮助类+Aspose.Cells生成Excel帮助类

    大部分功能逻辑都在,少量自定义异常类和扩展方法 ,可用类似代码自己替换 //EpPlus读取生成Excel帮助类+读取csv帮助类,epplus只支持开放的Excel文件格式:xlsx,不支持 xls ...

  7. CSharp设计模式读书笔记(22):策略模式(学习难度:★☆☆☆☆,使用频率:★★★★☆)

    策略模式(Strategy Pattern):定义一系列算法类,将每一个算法封装起来,并让它们可以相互替换,策略模式让算法独立于使用它的客户而变化,也称为政策模式(Policy). 模式角色与结构: ...

  8. Linux下一个C基本的编程----写进Blog在那之前

    展望2周的实习吧. 各种酸甜苦辣.由于公司只是广告.毛承保让我去.严重的歧视.想也想开,争夺.结果让它成为.还是把它写自己的学习经验,我有同样的希望和迷茫的同学.少走一点弯路.行.切入正题: 一.參考 ...

  9. Swift语言指南(二)--语言基础之注释和分号

    原文:Swift语言指南(二)--语言基础之注释和分号 注释 通过注释向自己的代码中注入不可执行的文本,作为你自己的笔记或提示.Swift编译器运行时会忽略注释. Swift的注释与C语言极其相似,单 ...

  10. Android结构分析Android智能指针(两)

    笔者:刘蒿羽 博客:http://blog.csdn.net/liuhaoyutz Android版本号:4.4.2 在上一篇文章中,我们分析了Android智能指针中的强指针sp,本文我们来分析弱指 ...