POJ 1503 Integer Inquiry 简单大数相加

简介: POJ 1503 Integer Inquiry 简单大数相加

Description


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

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
string doo(string str1,string str2){
    string s;
    int str11=str1.length();
    int str22=str2.length();
    if(str11>str22){
        for(int i=0;i<str11-str22;i++){
            str2="0"+str2;
        }
    }
    else{
        for(int i=0;i<str22-str11;i++){
            str1="0"+str1;
        }
    }
    str11=str1.length();
    int cmp=0;
    int cf=0;
    for(int i=str11-1;i>=0;i--){
        cmp=str1[i]-'0'+str2[i]-'0'+cf;
        cf=cmp/10;
        cmp=cmp%10;
        s=char(cmp+'0')+s;
    }
    if(cf!=0){
        s=char(cf+'0')+s;
    }
    return s;
}
int main(){
   string sum="0";
   string str;
   while(cin>>str){
        if(str=="0"){
            break;
        }
    sum=doo(sum,str);
   }
   cout<<sum<<endl;
   return 0;
}
目录
相关文章
|
算法
poj 2479 Maximum sum(求最大子段和的延伸)
看完最大连续子段和 的 dp算法 这个很容易理解,我用dplift[i]保存第1到第i个之间的最大子段和,dpright[i]保存第i到第n个之间的最大子段和,最终结果就是dplift[i]+dpright[i+1]中最大的一个。
54 0
|
网络架构
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
|
人工智能
【待补】UPC No Need(二分+bitset || 背包dp)
【待补】UPC No Need(二分+bitset || 背包dp)
60 0
All in the Family_upc_模拟 or lca + 并查集
The scientists working at the Family Genetics Institute are tracing the spread of a hereditary disease through family trees. They have started by listing the family members that have the disease,
124 0
All in the Family_upc_模拟 or lca + 并查集
POJ-2389,Bull Math(大数乘法)
POJ-2389,Bull Math(大数乘法)
HDOJ/HDU 2561 第二小整数(水题~排序~)
HDOJ/HDU 2561 第二小整数(水题~排序~)
118 0
POJ 1844 Sum
POJ 1844 Sum
106 0
|
Java 数据安全/隐私保护
[LintCode] Number of Islands(岛屿个数)
描述 给一个01矩阵,求不同的岛屿的个数。 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1] ] 中有 3 个岛。
1252 0
|
算法
洛谷 P1348 Couple number
题目描述 任何一个整数N都能表示成另外两个整数a和b的平方差吗?如果能,那么这个数N就叫做Couple number。你的工作就是判断一个数N是不是Couple number。 输入输出格式 输入格式: 仅一行,两个长整型范围内的整数$n_1$和$n_2$,之间用1个空格隔开。
1121 0