POJ 1503 && HDU 1047 Integer Inquiry(高精度)
2015-08-25 10:19
387 查看
Description
大整数加法,每个整数至多包含100个数字,最多100个整数相加,计算并输出结果
Input
最多输入100行数据,每行输入一个正整数,输入0结束输入
Output
输出所有正整数累加的结果
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670
Solution
简单模拟
用数组存正整数,从低位往高位存,加的时候注意进位
Code
大整数加法,每个整数至多包含100个数字,最多100个整数相加,计算并输出结果
Input
最多输入100行数据,每行输入一个正整数,输入0结束输入
Output
输出所有正整数累加的结果
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670
Solution
简单模拟
用数组存正整数,从低位往高位存,加的时候注意进位
Code
#include<stdio.h> #include<string.h> #define maxn 105 int main() { int map[maxn][maxn]; memset(map,0,sizeof(map));//初始化 char s[maxn]; int k=0; while(gets(s)&&strcmp(s,"0")!=0)//以字符串形式读入整数 { int len=strlen(s); for(int i=len-1;i>=0;i--)//将字符串转化为整型数组存储 map[k][len-i-1]=s[i]-'0'; k++;//记录正整数数量 } int temp=0,ans[maxn];//temp为每次进位值,ans存储最终答案 for(int i=0;i<maxn;i++) { int res=0; for(int j=0;j<k;j++) res+=map[j][i]; ans[i]=(res+temp)%10;//记录答案 temp=(res+temp)/10;//记录进位值 } int i=maxn-1; while(ans[i]==0)//判断答案位数 i--; for(;i>=0;i--) printf("%d",ans[i]); printf("\n"); return 0; }
相关文章推荐
- UGUI(九)UI粒子特效
- UGUI(八)UI节点制作
- 【php】include、include_once、require、require_once的区别
- UGUI(七)界面拖动和焦点界面
- QtGui.QComboBox控件常用函数
- Android Studio failed to find build tools revision 19.1.0 问题
- leetcode: (232) Implement Queue using Stacks
- UGUI(六)资源下载之Icon
- UGUI(五)动画系统
- UGUI(四)事件系统的封装
- 解决 ffmpeg yasm not found, use --disable-yasm for a crippled build
- Request(对象)
- UGUI(三)无限循环列表
- The request sent by the client was syntactically incorrect
- UITableViewCell自定义右边的文字以及更改左边图片的大小
- UGUI(二)摄像机渲染纹理
- request
- UGUI(一)List列表的封装
- 【IOS】高仿网易4.0新UI框架
- Gradle build设置自动log开关