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;
}

另外二种方法

#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <string.h> char a[200],b[200],c[200],d[200];
int main()
{
int n1,n2;
b[0]='0';
while(1){
scanf("%s",a);
if(strcmp(a,"0")==0){
printf("%s\n",b);
return 0;
}
int i;
int n1=strlen(a)-1;
int n2=strlen(b)-1;
int p=0;
for(i=0;n1>=0||n2>=0;i++,n1--,n2--)
{
if(n1>=0&&n2>=0){c[i]=a[n1]+b[n2]-'0'+p;}
if(n1>=0&&n2<0){c[i]=a[n1]+p;}
if(n1<0&&n2>=0){c[i]=b[n2]+p;}
p=0;
if(c[i]>'9'){c[i]=c[i]-10;p=1;}
}
if(p==1){
c[strlen(c)]='1';
}
for(int i=0;i<strlen(c);i++){
d[strlen(c)-i-1]=c[i];
} strcpy(b,d); }
}

——————————————————

#include <stdio.h>
#include <stdlib.h>
#include <string.h> char a[200],b[200],c[200];
int main()
{
//int n1,n2;
b[0]='0';
while(~scanf("%s",a))
{
if(strcmp(a,"0")==0)
{
printf("%s\n",b);
return 0;
}
int i;
int n1=strlen(a)-1;
int n2=strlen(b)-1;
int p=0;
i=n1;
if(i<n2)
i=n2;
for(i; n1>=0||n2>=0; i--,n1--,n2--)
{
if(n1>=0&&n2>=0)
{
c[i]=a[n1]+b[n2]-'0'+p;
}
if(n1>=0&&n2<0)
{
c[i]=a[n1]+p;
}
if(n1<0&&n2>=0)
{
c[i]=b[n2]+p;
}
p=0;
if(c[i]>'9')
{
c[i]=c[i]-10;
p=1;
}
}
if(p==1)
{ for(i=strlen(c); i>0; i--)
c[i]=c[i-1];
c[0]='1';
}
strcpy(b,c);
}
}

POJ 1503 Integer Inquiry 简单大数相加的更多相关文章

  1. POJ 1503 Integer Inquiry(大数相加,java)

    题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main ...

  2. POJ 1503 Integer Inquiry(大数相加)

    一.Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exp ...

  3. Poj 1503 Integer Inquiry

    1.链接地址: http://poj.org/problem?id=1503 2.题目: Integer Inquiry Time Limit: 1000MS   Memory Limit: 1000 ...

  4. UVa 424 Integer Inquiry 【大数相加】

    解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当 ...

  5. POJ 1503 Integer Inquiry 大数 难度:0

    题目链接:http://poj.org/problem?id=1503 import java.io.*; import java.math.BigInteger; import java.util. ...

  6. poj 1503 Integer Inquiry (高精度运算)

    题目链接:http://poj.org/problem?id=1503 思路分析: 基本的高精度问题,使用字符数组存储然后处理即可. 代码如下: #include <iostream> # ...

  7. Integer Inquiry【大数的加法举例】

    Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27730   Accepted: 10764 ...

  8. 九度OJ 1119:Integer Inquiry(整数相加) (大数运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:679 解决:357 题目描述: One of the first users of BIT's new supercomputer was ...

  9. hdu 1047 Integer Inquiry(大数)

    题意:整数大数加法 思路:大数模板 #include<iostream> #include<stdio.h> #include<stdlib.h> #include ...

随机推荐

  1. A题笔记(3)

    No. 1381 容器相关 #include <vector> 头文件 vector<Presents> present; present.push_back(name); 向 ...

  2. Java的split方法说明

    相信大家都经常使用String 的split方法,但是大家有没有遇到下面的这种情况: 大家想想下面的代码执行结果是什么 public static void main(String[] args) { ...

  3. object-C 手动内存管理(MRC)

    object-C的内存管理和javascript的垃圾回收不一样,今天总结下手动内存管理,ARC的后边补上. 1:基本铺垫 oc采用引用计数来表示对象的状态,比如通过init创建出来的一个对象引用计数 ...

  4. C# DataTable的詳細用法 - hcw_peter的专栏 - 博客频道 - CSDN

    C# DataTable的詳細用法 - hcw_peter的专栏 - 博客频道 - CSDN.NET 在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够 ...

  5. javascript之事件详解2

    1.事件对象: 在触发DOM事件的时候都会产生一个对象. 2.事件对象event: (1).type:获取事件类型 (2).target:获取事件目标 (3).stopPropagation():阻止 ...

  6. angularJS广播

    控制器之间共享数据(向父级/子级控制器传递event,data),类似于service在不同的控制器中通信 html: <div ng-controller="ParentCtrl&q ...

  7. GCC选项-Xlinker和-Wl区别

    写下给自己备忘,在一次使用GCC的过程中发现了原来传递给链接器ld可以同时使用Xlinker和Wl两种命令,这两个命令都可以正确传递给ld作为使用,现在总结下两者的区别. Xlinker后面跟的参数第 ...

  8. R.Net 用法

    using (REngine engine = REngine.CreateInstance("RDotNet")) { engine.Initialize(); var expa ...

  9. CSS2简写代码(优化)

    [1]如果CSS属性值为0,那么你不必为其添加单位(如:px/em): 下面是你可能的写法: padding: 10px 5px 0px 0px; 但是你可能这样写: padding: 10px 5p ...

  10. GCC编译器的安装

    1.GCC简介 GCC(GNU Compiler Collection)是一套功能强大.性能优越的编程语言编译器,它是GNU计划的代表作品之一.GCC是Linux平台下最常用的编译器,GCC原名为GN ...