题记(10)--大数加法

发布时间:2024年01月10日

目录

一、题目内容

二、输入描述

三、输出描述

四、输入输出示例

五、完整C语言代码


一、题目内容

? 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. 注意输入数据中,VeryLongInteger 可能有前导0

三、输出描述

Your program should output the sum of the VeryLongIntegers given in the input.

四、输入输出示例

输入:

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

输出:

370370367037037036703703703670

五、完整C语言代码

AC代码~

#include<stdio.h>
#include<string.h>
int main()
{
    char str1[101]; // 将str1作为中间结果
    char str2[101];
    int ans[101]={0}; // 中间结果的int表示
    scanf("%s",str1);
    while(scanf("%s",str2) != EOF)
    {
        if(!strcmp(str2,"0"))
            break;
        else
        {
            int num1[101]={0};
            int num2[101]={0};
            int n1 = strlen(str1);
            int n2 = strlen(str2);
            //将str1和str2从低位到高位存入num1和num2
            for(int i = 0;i<n1;i++)
                num1[i] = str1[n1-1-i] - '0';
            for(int i = 0;i<n2;i++)
                num2[i] = str2[n2-1-i] - '0';
            int n = n1>n2?n1:n2;
            int c = 0;
            for(int i = 0;i<n;i++)
            {
                ans[i] = (num1[i] +num2[i] + c) %10;
                c = (num1[i] +num2[i] + c) /10;
            }
            if(c) // 最高位有进位
            {
                ans[n] = c;
                n++;
            }
            for(int i = 0;i<n;i++)
                str1[i] = ans[n-1-i]+'0';
        }
    }
    puts(str1);
    return 0;
}

文章来源:https://blog.csdn.net/ADDDDDDS/article/details/135468386
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。