程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 424 Integer Inquiry(整數查詢)

424 Integer Inquiry(整數查詢)

編輯:C++入門知識

 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

 

 

 

題目大意:

  大整數相加,實際上像是小學的豎式求和,一位一位的相加就可以了。

注意點:WA了兩次,後來發現,原來最後輸出之後,要有個換行符!!!

後來發現,不一定需要判斷最後的一行是0,可以一直讀到EOF。

 


下面貼出兩種不同想法的代碼:

代碼一:

將所有行都讀完,再來進行大數計算。

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INF 1e9

char str[105][105];
int num[105][105];
int ans[10010];

int main(){
    int i, j;
    int len, count, max = -INF, flag;

    i = 0;
    while(~scanf("%s", str[i])){
        if(str[i][0] == '0')  break;
        i++;
    }

    count = i;
    memset(num, 0, sizeof(num));
    for(i = 0; i<count; i++){
        len = strlen(str[i]);
        for(j = 0; j<len; j++){
            num[i][len-j] = (str[i][j]-'0');
        }
        if(max<len)
            max = len;
    }

    memset(ans, 0, sizeof(ans));

    for(i = 0; i<count; i++){
        for(j = 1; j<=max; j++){
            ans[j] += num[i][j];
        }
    }

    for(i = 1; i<10010;i++){
        ans[i] += ans[i-1]/10;
        ans[i-1] %= 10;
    }

    flag = 0;
    for(i=10009; i>0; i--){
        if(flag){
            printf("%d", ans[i]);
        }
        else if(ans[i]){
            flag = 1;
            printf("%d", ans[i]);
        }
    }
    printf("\n");
    return 0;
}

 


代碼二:
沒讀一行字符串,進行一次大數相加。

 

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

char str[100];
int num[100];
int ans[10000];

int main(){
    int i, j;
    int len, flag;

    memset(ans, 0, sizeof(ans));
    while(~scanf("%s", str)){
        len = strlen(str);
        memset(num, 0, sizeof(num));
        for(i = 0; i<len; i++)
            num[len-i] = str[i]-'0';

        for(i = 1; i<=len; i++)
            ans[i] += num[i];

        for(i = 1; i<10000; i++){
            ans[i] += (ans[i-1]/10);
            ans[i-1] %= 10;
        }
    }

    flag = 0;
    for(i=10000-1; i>0; i--){
        if(flag)
            printf("%d", ans[i]);
        else if(ans[i]){
            printf("%d", ans[i]);
            flag = 1;
        }
    }
    printf("\n");
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved