HDU 1002大数加法

xiaoxiao2021-02-27  220

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 364041 Accepted Submission(s): 70825

Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input 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.

Output 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. Output a blank line between two test cases.

Sample Input 2 1 2 112233445566778899 998877665544332211

Sample Output Case 1: 1 + 2 = 3

Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110


思路

1、利用字符串方式存储数据

2、根据字符1与int1的ascll相差48转换

3、按照逐位相加的方法两个数组进行相加(重要)

4、输出

5、注意格式(重要)


C解法

#include <stdio.h> #include <string.h> int main() { int i,h; scanf("%d",&h);//输入你要循环加法的次数 for(i=1;i<=h;i++) { char s1[1001],s2[1001];//高精度计算需要将数据首先存储在字符串中 int a[1001]={0},b[1001]={0},c[1001]={0}; int len1,len2,len3;//保存字符串的长度 int max;//得到最大长度字符串 int q,k;//字符串转化int型数组变量 int m,d;//数组相加的数组变量 int l; scanf("%s %s",s1,s2); len1=strlen(s1); len2=strlen(s2); if(len1>len2) { max=len1; } else { max=len2; } /////////////字符串转化为int型/////////////// for(q=0,k=len1-1;q<len1;q++,k--)//注意这里字符串末尾数据放到了int数组开头 { a[q]=s1[k]-48; } for(q=0,k=len2-1;q<len2;q++,k--) { b[q]=s2[k]-48; } //////////数组相加/////////// for(d=0,m=0;m<max;m++) { c[m]=(a[m]+b[m]+d)%10; d=(a[m]+b[m]+d)/10; } printf("Case %d:\n%s + %s = ",i,s1,s2); if(d!=0) { c[max]=1; for(len3=max;len3>=0;len3--) { printf("%d",c[len3]); } } else { for(len3=max-1;len3>=0;len3--) { printf("%d",c[len3]); } } if(i!=h) { printf("\n\n"); } else { printf("\n"); } } return 0; }

C++解法(OJ语言选G++)

#include <iostream> #include <string.h> using namespace std; int main() { int i,h; cin>>h; for(i=1;i<=h;i++) { char s1[1001],s2[1001]; int a[1001]={0},b[1001]={0},c[1001]={0}; cin>>s1>>s2; int len1,len2; len1=strlen(s1); len2=strlen(s2); int max; if(len1>len2) { max=len1; } else { max=len2; } for(int q=0,m=len1-1;q<len1;q++,m--) { a[q]=s1[m]-48; } for(int q=0,m=len2-1;q<len2;q++,m--) { b[q]=s2[m]-48; } int d; for(int k=0,d=0;k<max;k++) { c[k]=(a[k]+b[k]+d)%10; d=(a[k]+b[k]+d)/10; } cout<<"Case"<<" "<<i<<":"<<"\n"<<s1<<" "<<"+"<<" "<<s2<<" "<<"="<<" "; if(d!=0) { c[max]=1; for(int len3=max;len3>=0;len3--) { cout<<c[len3]; } } else { for(int len3=max-1;len3>=0;len3--) { cout<<c[len3]; } } if(i!=h) { cout<<"\n\n"; } else { cout<<"\n"; } } return 0; }

总结

1、一般来说,大数都是用字符串来储存,一个字符表示一个数位。大数就是位数多,数值大的意思。通常来说c语言里的基本数据类型范围是有限的,如long型的最大只能表示几十亿,几十亿也就11位数字长度而已。如果用100长的数组表示,假设数组一个元素存数字的一位,那么总位数可以达到100位,这是基本数据类型无法表示的。

2、基本类型的数据范围: [signed]int :-32768—-32767 unsigned int:0—65535 long[int]:-2^31—-(2^31-1) unsigned long [int]:0—(2^32-1) float:10^-37—–10^38 double:10^-307—–10^308 long double:10^-4931—-10^4932

转载请注明原文地址: https://www.6miu.com/read-7317.html

最新回复(0)