Integer Inquiry UVA 424

#include <stdio.h>
#include <string.h>
#define MAXN 100+5
#define MAXL 100+5
char addend[MAXN][MAXL];//保存加数
char ans[MAXL];//保存结果
int len[MAXN];//保存每个加数的长度

int main(){
 int cnt=0;//加数的个数
 int max=0;//最长的加数的长度
 int i,k,j=MAXL-1,sum,carry;
 //freopen("data","r",stdin);
 while(scanf("%s",addend[cnt])&&addend[cnt][0]!='0'){
  len[cnt]=strlen(addend[cnt]);
  max=max>len[cnt]?max:len[cnt];
  cnt++;
 }

 ans[j--]='\0';
 carry=0;
 for(i=0;i<max;i++){//从最低位开始对每个加数的对应位相加
  sum=carry;
  for(k=0;k<cnt;k++)
   if(len[k]-i>0)//保证所加位在该加数的长度内
   sum+=addend[k][len[k]-i-1]-'0';

 ans[j--]=sum%10+'0';//确定结果的对应位
 carry=sum/10;
 }

while(carry){//将最后的进位,注意也许并非一位数,依次放入结果中
ans[j--]=carry%10+'0';
carry/=10;
}

printf("%s\n",ans+j+1);

return 0;

}

Integer Inquiry UVA 424

时间: 2024-11-05 18:39:31

Integer Inquiry UVA 424的相关文章

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

424 - Integer Inquiry

 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.

hdu1047 Integer Inquiry

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

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

HDOJ 1047 Integer Inquiry

JAVA睑板.... Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12042    Accepted Submission(s): 3037 Problem Description One of the first users of BIT's new supercomputer was Chip D

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

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

HDU - 1047 - Integer Inquiry (大数高精度)

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