A+B Problem II 大数加法
2017-02-10 00:00
246 查看
A+B Problem II
http://acm.nyist.net/JudgeOnline/problem.php?pid=103时间限制:3000 ms | 内存限制:65535 KB
难度:3
输入
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
输出
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation.
样例输入
2 1 2 112233445566778899 998877665544332211
样例输出
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
描述
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
A,B must be positive.
我的代码
#include <stdio.h> #define MAX 1010 char a[MAX],b[MAX],c[MAX]; int main(int argc, char *argv[]) { int T,t,d,i,j,x,y,N=0; scanf("%d",&T); while(T--){ for(i=0;i<MAX;i++) a[i]=b[i]=c[i]='0'; //将字符串改为 数字低位在数组的低位 scanf("%s",c); for(x=0;c[x];x++); for(i=0;i<x;i++) a[i]=c[x-1-i]; a[i]='0'; scanf("%s",c); for(x=0;c[x];x++); for(i=0;i<x;i++) b[i]=c[x-1-i]; b[i]='0'; for(i=0;i<MAX;i++) c[i]='0'; c[i]='0'; //加法运算 d=0; //进位 for(i=0;i<MAX;i++){ t = a[i]+b[i]-2*'0'+d; if(t>9){ d=1; c[i]=t%10+'0'; }else{ d=0; c[i]=t+'0'; } } //输出 printf("Case %d:\n",++N); //考虑 0+0=0 for(i=MAX-1;a[i]=='0'&& i;i--); for(;i>=0;i--) printf("%c",a[i]); printf(" + "); for(i=MAX-1;b[i]=='0'&& i;i--); for(;i>=0;i--) printf("%c",b[i]); printf(" = "); for(i=MAX-1;c[i]=='0' && i;i--); for(;i>=0;i--) printf("%c",c[i]); printf("\n"); } return 0; }
简单做法
#include <stdio.h> #include <string.h> #define MAX 1020 char a[MAX],b[MAX],c[MAX]; int main(int argc, char *argv[]) { int n=0,T,t,i,j,d,lena,lenb,lenc; scanf("%d",&T); while(T--){ scanf("%s",a); scanf("%s",b); lena=strlen(a); lenb=strlen(b); printf("Case %d:\n",++n); printf("%s + %s = ",a,b); d=lenc=0; i=lena-1; j=lenb-1; while(i>=0 && j>=0){ t=a[i--]+b[j--]-2*'0'+d; if(t>9){ d=1; c[lenc++]=t%10; }else{ d=0; c[lenc++]=t; } } while(i>=0){ t=a[i--]-'0'+d; if(t>9){ d=1; c[lenc++]=t%10; }else{ d=0; c[lenc++]=t; } } while(j>=0){ t=b[j--]-'0'+d; if(t>9){ d=1; c[lenc++]=t%10; }else{ d=0; c[lenc++]=t; } } if(d) c[lenc++]=1; for(t=lenc-1;t>=0;t--) printf("%d",c[t]); printf("\n"); } return 0; }
题目推荐
import java.math.BigInteger; import java.util.Scanner; public class Main{ public static void main(String args[]) { Scanner cin=new Scanner(System.in); int n=cin.nextInt(); BigInteger a,b; for(int i=1;i<=n;i++){ a=cin.nextBigInteger(); b=cin.nextBigInteger(); System.out.println("Case "+i+":"); System.out 7fe0 .println(a.toString()+" + "+b.toString()+" = "+a.add(b)); } } }
相关文章推荐
- 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
- A + B Problem II(大数加法hd1002)
- HDU A + B Problem II(大数加法)
- hdoj 1002 A + B Problem II【大数加法】
- 1001 A + B Problem II(C语言 大数加法模板)
- hdu1002 A + B Problem II(大数加法一)模板
- A + B Problem II(大数基础加法)
- A + B Problem II(大数加法)
- A + B Problem II (大数加法)
- NYOJ 880 A+B Problem II(大数加法)
- A + B Problem II(大数加法)
- HDU 1002 A + B Problem II(大数加法,C,Java两个版本)
- hdoj 1002 A + B Problem II(大数加法)
- ACMSTEP 2.3.1 A+B Problem II //大数加法
- 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
- A + B Problem II 大数加法
- A + B Problem II(hdu1002,大数加法)
- A + B Problem II(大数加法)
- hdoj 1002 A + B Problem II 【大数加法模板】
- hduoj1002,A + B Problem II, 大数加法,经典