欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【HDUOJ】第1002题 A + B Problem II 纯C语言解法

程序员文章站 2023-12-31 20:42:46
...

【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。

  1. void * memset(void * s,int ch,size_t count)  
  2. {  
  3.     char *xs = (char *) s;  
  4.     while (count--)  
  5.         *xs++ = ch;  
  6.     return s;  
  7. }  

3:完整代码(已AC

#include <stdio.h>
#include <string.h>
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<len1;i++)            a[i]=m[len1-i-1]-'0';
        for(int j=0;j<len2;j++)
            b[j]=n[len2-j-1]-'0';
        //将字符转换为integer型数字并存入数组
        for( len=0;len<max;len++){
            if(a[len]+b[len]+c[len]>=10){
                c[len]+=a[len]+b[len]-10;
                c[len+1]++;
            }
            else
                c[len]+=a[len]+b[len];
        }
        for(l=len;l<max;l++){
            if(a[l]+c[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++<T)
            printf("\n\n");
        else
            printf("\n");
    }
    return 0;
}


上一篇:

下一篇: