Recently suddenly want to go to the algorithm direction, do the avionics ACM several problems
Apart, start
Avionics ACM 1002 problem is mainly to deal with long data problems, the algorithm principle is relatively simple, is to use a character array instead of int, because int is too short to process the data longer
The following is a description of the problem:
problem DescriptionI have a very simple problem for you. Given integers A and B, your job is to calculate the Sum of A + B.
InputThe 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 both positive integers, A and B. Notice that the integers is very large, that M EANs should not process them by using 32-bit integer. Assume the length of each integer would not exceed.
Outputfor the test case, you should output of the lines. The first line was "Case #:", # means the number of the the test case. The second line is the equation "A + b = sum", sum means the result of A + B. Note there is some spaces int the Equati On. Output a blank line between the test cases.
Sample Input2 1 2 112233445566778899 998877665544332211
Sample OutputCase 1:1 + 2 = 3 Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
AuthorIGNATIUS.L
My Code reference http://blog.csdn.net/odaynot/article/details/8049632 is listed below
#include <stdio.h>#include<string.h>intToInt (Charc) { returnC-'0'; } intMain () {intI, n,j=1, al,bl,ml,t; Chara[ +], b[ +];//two number of stored inputs intcount[1001]; scanf ("%d",&N); while(n--){ if(j!=1) printf ("\ n"); scanf ("%s", a); Al=strlen (a);//returns the number of characters before the string Terminatorscanf"%s", B); BL=strlen (b); ML= (AL>BL)? AL:BL;//get a longer character lengtht=ml; for(i=0; i<=ml;i++) count[i]=0;//initializing an array for(ml;al>0&&bl>0; ml--) {COUNT[ML]+=toint (A[--al]) +toint (b[--BL]); if(count[ml]/Ten){ //Handling Rounding Problemscount[ml-1]++; COUNT[ML]=count[ml]%Ten; } } while(al>0) {count[ml--]+=toint (a[--al]); if(count[ml+1]/Ten) {COUNT[ML]++; COUNT[ML+1]%=Ten; } } while(bl>0) {count[ml--]+=toint (b[--BL]); if(count[ml+1]/Ten) {COUNT[ML]++; COUNT[ML+1]%=Ten; }} printf ("Case %d:\n%s +%s =", J + +, A, b); for(i=0; i<=t;i++){ if(i==0&&count[i]==0) {i++; } printf ("%d", Count[i]); } printf ("\ n"); } return 0; }
welcome criticism, questions please leave a message
ACM 1002 Algorithm Design