C语言oj中a b怎么做,【HDUOJ】第1002题 A + B Problem II 纯C语言解法

【HUDOJ-1002】

1.原题:

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

21 2112233445566778899 998877665544332211

Sample Output

Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110

Author

Ignatius.L

2:解题思路及技巧:

在计算机中,当要表示的数据超出计算机所使用的数据的表示范围时,则产生数据的溢出。溢出导致大数相加计算错误,此时就需要一种新的办法。由于大数无法直接保存,我们采用字符串的方式保存大数的每一位,然后将字符转换为数字型,类似于小学加法算式将两个数对应位相加(大于十进一),这里需要注意以下几点:

a.字符转数字:遍历数组对每一位减‘0’得到对应数字的integer值。

b.要初始化所有数组的所有元素为0以便后续的计算,这里采用memset函数将数组的所有元素初始化为0。

c.存数时需要倒序输入便于计算。

d.memset函数:memset是计算机中C/C++语言函数。将s所指向的某一块内存中的后count个字节的内容全部设置为ch指定的ASCII值, 第一个值为指定的内存地址,块的大小由第三个参数指定,这个函数通常为新申请的内存做初始化工作, 其返回值为s。

void* memset(void* s,intch,size_tcount)

{

char*xs = (char*) s;

while(count--)

*xs++ = ch;

returns;

}

3:完整代码(已AC)

#include #include int main(int argc,char const*argv[])

{

int T;

char m[1001],n[1001];

scanf("%d",&T);

int cnt=1;

int k=T;

while(k--){

int a[1001],b[1001],c[1001];

memset(a,0,sizeof(a));

memset(b,0,sizeof(b));

memset(c,0,sizeof(c));

//初始化数组元素为零

int l,len;

scanf("%s",&m);

scanf("%s",&n);

//读入字符数组

int len1=strlen(m),len2=strlen(n);

//保存字符型数字的长度

int max=(len1>len2)?len1:len2;

for(int i=0;i=10){

c[len]+=a[len]+b[len]-10;

c[len+1]++;

}

else

c[len]+=a[len]+b[len];

}

for(l=len;l=10){

c[l]+=a[l]-10;

c[l+1]++;

}

else

c[l]+=a[l];

}

printf("Case %d:\n",cnt);

for(int i=len1-1;i>=0;i--)

printf("%d",a[i]);

printf(" + ");

for(int i=len2-1;i>=0;i--)

printf("%d",b[i]);

printf(" = ");

for(int x=l-1;x>=0;x--)

printf("%d",c[x]);

if(cnt++

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值