HDU 1047 大数相加
2015-08-28 14:24
357 查看
Integer Inquiry
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15694 Accepted Submission(s): 4027
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.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Sample Input
1
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670反过来时顺便转成int。。。还有格式问题 还有就是a1每次都要初始化 否则影响接下来结果#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
char s[1005];
int a1[1005]={0};
int a2[1005]={0};
int main()
{
int n=0;
scanf("%d",&n);
getchar();
while(n--)
{
int k2=1;
memset(s,0,sizeof(s));
memset(a1,0,sizeof(a1));
memset(a2,0,sizeof(a2));
while(gets(s))
{
int len=strlen(s);
int i=0,k1=0;
if(s[0]=='0')
break;
for(i=len-1;i>=0;i--)
{
a1[k1++]=s[i]-'0';
}
int max=k1>k2?k1:k2;
int k=0;
int j=0;
for(j=0;j<max;j++)
{
a2[j]=a2[j]+a1[j]+k;
k=a2[j]/10;
a2[j]%=10;
}
memset(a1,0,sizeof(a1));
if(k)
{
a2[j]=k;
k2=j+1;
}
else
{
k2=j;
}
}
for(int i=k2-1;i>=0;i--)
{
printf("%d",a2[i]);
}
printf("\n");
if(n!=0)
printf("\n");
}
return 0;
}
相关文章推荐
- Android manifest文件中的标签详细介绍
- 关于UIAutomator 滚动的steps
- HDU 1004 Let the Balloon Rise(map)
- 为什么面试喜欢问算法问题
- SharePoint Server 2016 Preview 发布
- python 自动化运维 监测web质量
- EventBus详解
- java中ThreadLocal类的使用
- linux 下安装、使用 redis
- Leetcode Database - 我的汇总
- 事件与委托的关系
- JAVA泛型详解
- hdu1045
- EventBus详解
- 100个 iOS 程序员面试题汇总
- 单例模式
- 织梦调用文章摘要标签
- Android AsyncTask源码解析
- mybatis 中#与$的区别
- 理解和使用alsa配置-默认静音,必须先用amixer解除主音量和pcm音量的静音