UVA - 424 Integer Inquiry (高精度运算) (day_4_I)

版权声明:商业转载请联系作者获得授权,非商业转载请注明出处。 https://blog.csdn.net/weixin_43939327/article/details/86633418

Integer Inquiry

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.)
Input
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.
Output
Your program should output the sum of the VeryLongIntegers given in the input.
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670

题目简述:

给出不多于100个不多于100位的正整数,求和。

题目分析:

只有加法的高精度运算。做题时,因为题目只要求一组输入输出,所以最后没加换行符,WA了十几次,不给PE太过分了QAQ

代码实现:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<queue>
//#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;

#define ms(i,j) memset(i,j,sizeof(i))

int a[1100];
int main()
{
    char s[1050];
    while(cin>>s&&!(s[0]=='0'&&strlen(s)==1))
    {
        for(int i=1; i<=strlen(s); i++)
            a[i]+=s[strlen(s)-i]-48;
        for(int i=1; i<1008; i++)
        {
            a[i+1]+=a[i]/10;
            a[i]=a[i]%10;
        }
        ms(s,0);
    }
    int i;
    for(i=1008; i>0&&a[i]==0; i--);
    if(i==0) cout<<0;
    for(;i>0;i--)
        cout<<a[i];
    cout<<endl;
}

猜你喜欢

转载自blog.csdn.net/weixin_43939327/article/details/86633418
今日推荐