【大数加法】POJ-1503、NYOJ-103

1503:Integer Inquiry

总时间限制: 
1000ms
 
内存限制: 
65536kB
描述
One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. 
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.) 
输入
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative). 

The final input line will contain a single zero on a line by itself. 
输出
Your program should output the sum of the VeryLongIntegers given in the input.
样例输入
123456789012345678901234567890

123456789012345678901234567890

123456789012345678901234567890

0
样例输出
370370367037037036703703703670
  • 先说说为啥要用算法实现大数加法:

    我们来看看数据:

    bool型为布尔型,占1个字节,取值0或1。

    BOOL型为int型,一般认为占4个字节,取值TRUE/FALSE/ERROR。

    sbyte型为有符号8位整数,占1个字节,取值范围在128~127之间。

    bytet型为无符号16位整数,占2个字节,取值范围在0~255之间。

    short型为有符号16位整数,占2个字节,取值范围在-32,768~32,767之间。

    ushort型为无符号16位整数,占2个字节,取值范围在0~65,535之间。

    int型为有符号32位整数,占4个字节,取值范围在-2,147,483,648~2,147,483,647之间。

    uint型为无符号32位整数,占4个字节,取值范围在0~4,294,967,295之间。

    long型为64位有符号整数,占8个字节,取值范围在9,223,372,036,854,775,808~9,223,372,036,854,775,807之间。

    ulong型为64位无符号整数,占8个字节,取值范围在0~18,446,744,073,709,551,615之间。

    float型为32位单精度实数,占4个字节,取值范围3.4E+10的负38次方~3.4E+10的38次方之间。

    double型为64位实数,占8个字节,取值范围1.7E+10的负308次方~1.7E+10的正308次方。

 1 int num1[MAX],num2[MAX];

 2 void Add(char a[],char b[]){

 3     int i,j,len1,len2,max;    

 4     memset(num1,0,sizeof(num1));

 5     memset(num2,0,sizeof(num2));

 6     len1 = strlen(a);

 7     len2 = strlen(b);

 8     //获得最长数组的长度

 9     //两数相加最多不会超过最大数的位数+1,减少循环次数

10     max = len1 > len2 ? len1 : len2;

11     //将两字符串数组转换为数字数组

12     for(i = len1 - 1,j = 0;i >= 0;i--){

13         num1[j++] = a[i] - '0';

14     }

15     for(i = len2 - 1,j = 0;i >= 0;i--){

16         num2[j++] = b[i] - '0';

17     }

18     //模拟进位运算

19     for(i = 0;i <= max;i++){

20         num1[i] += num2[i];

21         if(num1[i] > 9){

22             num1[i] -= 10;

23             num1[i + 1] ++;

24         }

25     }

26     //输出

27     int ac = 0;

28     for(i = max;i >= 0;i--){//输出排除0

29         if(num1[i] != 0)

30             ac = 1;

31         if(ac){

32             printf("%d",num1[i]);        

33         }

34     }

35 }
  • 感觉学会了,于是乎,就找了题,继续练练手,就是POJ-1503.
  • 然后就陷入了深坑中,首先就是那个输入,竟然一直想要用二维数组,真是。。。只能怪练得还太少。
  • 解题过程中,脑中想的方法过于复杂,那肯定就是错误的,一定要跳出这个思维,想别的思路解决。
 1 //POJ-1503-1

 2 #include <cstdio>  

 3 #include <cstring>  

 4 const int MAXN = 200;  

 5 int main()  

 6 {  

 7     char s[MAXN]; 

 8     int i, j;

 9     int m;

10     scanf("%d",&m);

11     while(m--){

12         int sum[MAXN] = {0}; 

13         while(gets(s))  

14         {  

15             int len = strlen(s);  

16             if(s[0] == '0' && len == 1)  

17                 break;  

18             //sum[]保存所有组的各位的和

19             for(i = 110, j = len-1; j >= 0; i--, j--)  

20             {  

21                 sum[i] += s[j]-'0';  

22             }  

23         }  

24         //将sum[]每个元素,进行进位

25         for(i = 110; i > 0; i--)  

26         {  

27             sum[i-1] += sum[i] / 10;  

28             sum[i] %= 10;  

29         }  

30         //排除和为0的情况

31         for(i = 0; sum[i] == 0 && i < 111; i++)  

32         {  

33             if(i == 111)//意味着全为零  

34             {  

35                 printf("0\n");  

36             }  

37         }  

38         for( ; i < 111; i++)  

39         {  

40             printf("%d",sum[i]);  

41         }  

42         printf("\n\n"); 

43     }     

44     return 0;  

45 }

A+B Problem II

时间限制: 3000 ms  |  内存限制:65535 KB
难度: 3
 
描述

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.

 
输入
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
 1 //NYOJ-103

 2 #include<cstdio>

 3 #include<cstring>

 4 const int MAX = 1002;

 5 char str1[MAX],str2[MAX];

 6 int num1[MAX],num2[MAX];

 7 void Add(char a[],char b[]){

 8     int i,j,len1,len2,max;    

 9     memset(num1,0,sizeof(num1));

10     memset(num2,0,sizeof(num2));

11     len1 = strlen(a);

12     len2 = strlen(b);

13     //获得最长数组的长度

14     //两数相加最多不会超过最大数的位数+1,减少循环次数

15     max = len1 > len2 ? len1 : len2;

16     //将两字符串数组转换为数字数组

17     for(i = len1 - 1,j = 0;i >= 0;i--){

18         num1[j++] = a[i] - '0';

19     }

20     for(i = len2 - 1,j = 0;i >= 0;i--){

21         num2[j++] = b[i] - '0';

22     }

23     //模拟进位运算

24     for(i = 0;i <= max;i++){

25         num1[i] += num2[i];

26         if(num1[i] > 9){

27             num1[i] -= 10;

28             num1[i + 1] ++;

29         }

30     }

31     //输出

32     int ac = 0;

33     for(i = max;i >= 0;i--){//输出排除0

34         if(num1[i] != 0)

35             ac = 1;

36         if(ac){

37             printf("%d",num1[i]);        

38         }

39     }

40 }

41 int main(){

42     int n,i = 1;

43     scanf("%d",&n);

44     while(n--){

45         scanf("%s %s",str1,str2);    

46         printf("Case %d:\n%s + %s = ",i,str1,str2);

47         if(str1[0] == '0' && str2[0] == '0'){

48             printf("0\n");

49         }else{

50             Add(str1,str2);

51             printf("\n");        

52         }

53         i++;

54     }

55     return 0;

56 }
  • 减法、乘法、除法明天继续

 

你可能感兴趣的:(poj)