Integer Inquiry(大数相加)

Problem 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<stdio.h>
#include<string.h>
using namespace std;
char c[200],d[200],e[200];
void sum(char a[],char b[])
{
int al=strlen(a)-1;
int bl=strlen(b)-1;
int cl=0;
while(al>=0&&bl>=0)
{
c[cl]=c[cl]+a[al]+b[bl]-‘0‘*2;
c[cl+1]=c[cl+1]+c[cl]/10;
c[cl]=c[cl]%10;
al--;
bl--;
cl++;
}
while(al>=0)
{
c[cl]=c[cl]+a[al]-‘0‘;
c[cl+1]=c[cl+1]+c[cl]/10;
c[cl]=c[cl]%10;
al--;
cl++;
}
while(bl>=0)
{
c[cl]=c[cl]+b[bl]-‘0‘;
c[cl+1]=c[cl+1]+c[cl]/10;
c[cl]=c[cl]%10;
bl--;
cl++;
}
if(c[cl]==0)
cl--;
for(int i=cl,j=0; i>=0; i--,j++)
{
c[i]=c[i]+‘0‘;
d[j]=c[i];
//printf("%c",c[i]);
}
//printf("\n");
//for(int i=0; i<=cl; i++)
//{
//printf("%c",c[cl-i]+‘0‘);
//}printf("\n");
//return c[c1];
}
int main()
{
char a[150][150];
int i=0,j,k;
while(scanf("%s",a[i])!=EOF)
{
if(strcmp(a[i],"0")==0)
break;
i++;
}
strcpy(e,a[0]);
//if(strcmp(a[0],"0")==0)
//return 0;
for(j=1; j<i; j++)
{
memset(c,0,200);
sum(e,a[j]);
strcpy(e,d);
}
printf("%s\n",e);
return 0;
}

时间: 2024-10-11 04:02:50

Integer Inquiry(大数相加)的相关文章

hdu acm-1047 Integer Inquiry(大数相加)

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11678    Accepted Submission(s): 2936 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He ex

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

题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @param args */ public static void main(String[] args) throws Exception { // 定义并打开输入文件 Scanner cin = new Scanner(System.in); BigInteger a, sum

HDU 1047 Integer Inquiry 大数相加 string解法

本题就是大数相加,题目都不用看了. 不过注意的就是HDU的肯爹输出,好几次presentation error了. 还有个特殊情况,就是会有空数据的输入case. #include <stdio.h> #include <vector> #include <string.h> #include <algorithm> #include <iostream> #include <string> #include <limits.h

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 on

POJ 1053 Integer Inquiry (大数加法,还是Java大法好)

Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32674   Accepted: 12789 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

POJ 1053 Integer Inquiry &amp;&amp; HDOJ 1047 Integer Inquiry (大数加法)

题目链接(POJ):http://poj.org/problem?id=1503 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=1047 Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30856   Accepted: 12007 Description One of the first users of BIT's new su

Integer Inquiry——大数连加

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=14340 思路: 模拟手动加法运算 (1)大数用数组存储,逆序 (2)计算两个数每一位相加,然后进位 #include<stdio.h> #include<string.h> #define N 110 void add(); void reverse(); void summ(); int sum[N],b[N]; char a[N]; int l; int

POJ 1503 Integer Inquiry 大数 难度:0

题目链接:http://poj.org/problem?id=1503 1 import java.io.*; 2 import java.math.BigInteger; 3 import java.util.Scanner; 4 5 public class Main { 6 public static void main(String []args) throws IOException{ 7 Scanner scanner=new Scanner(System.in); 8 BigInt

UVa 424 Integer Inquiry 【大数相加】

解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当输入a的第一个字符为0的时候结束运算,输出结果.  Integer Inquiry  One of the firstusers of BIT's new supercomputer was Chip Diller. He extended his explorationof powers of 3