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 to go from 0 to 333 and he explored taking various sums of thosenumbers.
``Thissupercomputer is great,'' remarked Chip. ``I only wish Timothy were here to seethese results.'' (Chip moved to a new apartment, once one became available onthe third floor of the Lemon Sky apartments on Third Street.)
Input
The input willconsist of at most 100 lines of text, each of which contains a singleVeryLongInteger. Each VeryLongInteger will be 100 or fewer characters inlength, and will only contain digits (no VeryLongInteger will be negative).
The final inputline will contain a single zero on a line by itself.
Output
Your programshould output the sum of the VeryLongIntegers given in the input.
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670
#include<stdio.h>
#include<string.h>
#define max 1000
void add(char a[],char b[],char c[])
{
char m[max],n[max];
int i;
int len1 ,len2;
int flag=0;
memset(m,0,sizeof(m));
memset(n,0,sizeof(n));
len1=strlen(a);
len2=strlen(b);
for(i=0;i<len1;i++)
{
m[i]=a[len1-i-1]-'0';
}
for(i=0;i<len2;i++)
{
n[i]=b[len2-i-1]-'0';
}
for(i=0;i<=len1||i<=len2;i++)
{
c[i]=m[i]+n[i]+flag;
flag=c[i]/10;
c[i]=c[i]%10+'0';
}
}
void shuchu(char c[])
{
int i,j;
int len;
len=strlen(c);
for(i=len-1;c[i]=='0';i--);
for(j=i;j>=0;j--)
{
printf("%c",c[j]);
}
printf("\n");
}
int main()
{
char a[max],b[max],c[max];
int i,j,len,tag=1;
memset(b,0,sizeof(b));
for(j=1;j<200&&tag;j++)
{
scanf("%s",&a);
if(a[0]=='0')
tag=0;
memset(c,0,sizeof(c));
add(a,b,c);
len=strlen(c);
for(i=0;i<len;i++)
{
b[i]=c[len-i-1];
}
}
shuchu(c); }
最新文章
- vs多项目模板及add-in开发
- Ext.NET 4.1 最新版本破解
- Dancing Links初学记
- 社交APP经典死法18种,听野路子产品菜狗怎么说
- winForm 打印预览
- smarty模板的基础搭建
- 【转】四种常见的POST提交数据方式
- java -jdk配置1(环境变量配置)
- 从头开始学JavaScript (九)——执行环境和作用域
- .NET基础——运算符
- thinkphp3.2.3的使用心得之i方法(零)
- Codeforces555 B. Case of Fugitive
- WCF-Oracel适配器针对UDT的使用配置与注意事项
- Android 开发 框架系列 百度语音合成
- 8.1Python面向对象编程(一)
- 使用Nginx+Lua实现自定义WAF
- Spring Boot 添加JSP支持【转】
- HTML5和CSS3扁平化风格博客(基础篇)
- Python int() 函数
- java unsupported major.minor version 51.0 解决