Integer Inquiry (High accuracy)

Source: Internet
Author: User
Tags printf

Description one of the first users of BIT ' s new supercomputer was Chip Diller. He extended his exploration of powers's 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 for 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 would consist of lines of text, each of which contains a single verylonginteger. Each verylonginteger'll be is fewer characters in length, and would only contain digits (no verylonginteger would be n egative).

The final input line would contain a single zero in 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

Problem Solving Ideas:

Is the general high-precision addition problem, the only thing to note is that some of the input data will have a leading 0, the data is too dog. I have several rounds of WA. Therefore, the input end cannot be judged by the first of the string. You also have to decide whether the second is a terminator.

AC Code:

#include <iostream> #include <cstdio> #include <cstring> using namespace std;
const int MAXN = 1000;
int SUM[MAXN];
    void Sum (char str[maxn]) {int length, NUM[MAXN];
    memset (num, 0, sizeof (num));
    Length = strlen (str);
    for (int i = 0; i < length; i++) Num[i] = str[length-i-1]-' 0 ';
        for (int i = 0; i < maxn-1; i++) {Sum[i] + num[i];
            if (Sum[i] >=) {sum[i]%= 10;
        sum[i+1]++;
    }}} int main () {char STR[MAXN];
    memset (sum, 0, sizeof (sum));
        while (scanf ("%s", str)) {if (str[0] = = ' 0 ' && str[1] = = 0) break;
    Sum (str);
    } bool Isbegin = FALSE;
        for (int i = maxn-1; I >= 0; i--) {if (isbegin) printf ("%d", sum[i]);
            else if (Sum[i]) {printf ("%d", sum[i]);
        Isbegin = true;
    }} if (!isbegin) printf ("0");
    printf ("\ n"); REturn 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.