106 大整数相加

问题描述 :

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

输入说明 :

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

输入范例 :

2
1 2
112233445566778899 998877665544332211

输出范例 :

Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

 

思想:用字符数组储存个数,进行单位加法然后进位处理。

 

#include <stdio.h>
#include <string.h>

int main()
{
    int num, i, j, k;
    scanf("%d", &num);
    for (k = 0; k < num; k++)
    {
        char n[4000];
        char num1[2000];
        int count1 = 0;
        char num2[2000];
        int count2 = 0;
        char res[3000];
        int count3 = 0;
        if(k==0){
            getchar();      //首行输入 吸收一下回车
        }
        
        gets(n);
        int flag = 0;  //进位标志
        for (i = 0; i < strlen(n); i++) //第一个数字
        {
            if (n[i] != ' ')
            {
                num1[count1++] = n[i];
            }
            else
            {
                i++;
                break;
            }
        }
        for (; i < strlen(n); i++)  //第二个数字
        {
            if(n[i]==' '){  // oj给的最后一个算例 调了一个小时没找到问题在哪 原来是它输入不规范 第二个数后面有空格 给它特殊处理一下 
                break;
            }
            num2[count2++] = n[i];
            if(n[i]==' '){
                break;
            }
        }
        count1--;  //count指向了后一个数 所以还原一下
        count2--;
        while (count1>=0 && count2>=0)  //进行加法
        {
            int temp = num1[count1--] - '0' + num2[count2--] - '0' + flag;
            flag = 0;
            if (temp >= 10)  
            {
                flag = 1;
                temp %= 10;
            }
            res[count3++] = temp + '0';
        }
        if (count1==-1 && count2==-1 && flag)  //如果两个数都加完了,还往前进一位的情况
        {
            res[count3++] = flag+'0';
        }
        else
        {
            while (count1>=0)  //1数的高位 累加进位处理
            {
                int temp = num1[count1--] - '0' + flag;
                flag = 0;
                if (temp >= 10)
                {
                    flag = 1;
                    temp %= 10;
                }
                res[count3++] = temp + '0';
            }
            while (count2>=0)  //2数的高位 累加进位处理
            {
                int temp = num2[count2--] - '0' + flag;
                flag = 0;
                if (temp >= 10)
                {
                    flag = 1;
                    temp %= 10;
                }
                res[count3++] = temp + '0';
            }
        }
        printf("Case %d:\n",k+1);
        for(i = 0;i<strlen(num1);i++){
            printf("%c", num1[i]);
        }
        printf(" + ");
        for(i = 0;i<strlen(num2);i++){
            printf("%c", num2[i]);
        }
        printf(" = ");
        for (i = count3-1; i >=0; i--)
        {
            printf("%c", res[i]);
        }
        printf("\n\n");

    }
}

 

posted @ 2022-03-13 21:36  梦想是能睡八小时的猪  阅读(112)  评论(0)    收藏  举报