UVA 424 (13.08.02)
Integer Inquiry |
One of the first users of BIT's new supercomputer was Chip Diller. Heextended his explorationof powers of 3 to go from 0 to 333 and he explored taking various sumsof those numbers.
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy werehere to see theseresults.'' (Chip moved to a new apartment, once one became available onthe third floor of theLemon Sky apartments on Third Street.)
Input
The input will consist of at most 100 lines of text, each of whichcontains a single VeryLongInteger.Each VeryLongInteger will be 100 or fewer characters in length, and willonly 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
题意: 大数相加~ 做法: 看AC的代码, 顺带注释, 容易懂的~ 另注:写了两份, 第一份AC之后发现代码不完美, 999999999999999 + 1计算不来 虽然AC了, 但是是由于黑盒子数据太弱了, 于是又重写了一份, 终于可计算999999999999999 + 1了 原因出在: 一个是每次加完都判断进位, 导致没有考虑到进位的连锁反应, 如999999999999999 + 1, 会一直进位 所以, 后来改成了全部累加完后, 统一进行进位, 就无懈可击了~ AC代码(无懈可击版, 样例999999999999999 + 1可行):
#include<stdio.h>
#include<string.h> int ans[1001]; int main() {
char num[101];
char ch;
int len;
//初始化答案数组~
for(int i = 0; i < 1001; i++)
ans[i] = 0; while(gets(num) != NULL) {
//结束并输出的标志
if(num[0] == '0')
break;
len = strlen(num);
//num数组倒序
for(int i = 0; i < len/2; i++) {
ch = num[i];
num[i] = num[len - 1 - i];
num[len - 1 - i] = ch;
}
//先累加~
for(int i = 0; i < len; i++)
ans[i] += (num[i] - '0');
}
//跳出后, 把累加的数值进行处理, 该进位的进~
for(int i = 0; i <= 999; i++) {
ans[i+1] += ans[i] / 10;
ans[i] = ans[i] % 10;
}
//从末尾开始找, 找到第一个非零数位置
int pos;
for(int i = 1000; i >= 0; i--) {
if(ans[i] != 0) {
pos = i;
break;
}
}
//从该位置倒序输出~
for(int i = pos; i >= 0; i--)
printf("%d", ans[i]);
printf("\n");
return 0;
}
AC代码(有漏洞版, 很久以前写的, 比较搓, 样例999999999999999 + 1不可通过)
#include <stdio.h>
#include <string.h> int main(){ int i, t, l, n;
int Sum, sum[1001];
char add[101];
for (i = 0; i < 1001; i++)
sum[i] = 0;
while (gets(add)){ if (strcmp(add,"0") == 0)
break; l = strlen(add); //add字符数组的倒序:
for (i = 0; i < l/2; i++){
t = add[i];
add[i] = add[l-i-1];
add[l-i-1] = t;
}
//累加到sum数组:
for (i = 0; i < l; i++){
Sum = add[i]-'0' + sum[i];
sum[i+1] = Sum / 10 + sum[i+1];
sum[i] = Sum % 10;
}
} //处理一:对sum数组从最后面开始寻找第一个非零数,标记
for (i = 1000; i >= 0; i--){
if (sum[i] != 0){
n = i;
break;
}
} //处理二:倒序输出~(yes)
for (i = n; i >= 0; i--)
printf("%d",sum[i]);
printf("\n");
return 0;
}
UVA 424 (13.08.02)的更多相关文章
- UVA 465 (13.08.02)
Overflow Write a program that reads an expression consisting of twonon-negative integer and an ope ...
- UVA 10494 (13.08.02)
点此连接到UVA10494 思路: 采取一种, 边取余边取整的方法, 让这题变的简单许多~ AC代码: #include<stdio.h> #include<string.h> ...
- UVA 10106 (13.08.02)
Product The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input T ...
- UVA 10194 (13.08.05)
:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ...
- UVA 253 (13.08.06)
Cube painting We have a machine for painting cubes. It is supplied withthree different colors: blu ...
- UVA 573 (13.08.06)
The Snail A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...
- UVA 10499 (13.08.06)
Problem H The Land of Justice Input: standard input Output: standard output Time Limit: 4 seconds In ...
- UVA 10025 (13.08.06)
The ? 1 ? 2 ? ... ? n = k problem Theproblem Given the following formula, one can set operators '+ ...
- UVA 536 (13.08.17)
Tree Recovery Little Valentine liked playing with binary trees very much. Her favoritegame was con ...
随机推荐
- Page.ClientScript.RegisterStartupScript不执行问题
c#后台使用Page.ClientScript.RegisterStartupScript在前台注册一段脚本提示,发现没有效果,寻寻觅觅,终于从度娘处找到了原因: 该页面多次使用到了Page.Clie ...
- 菜鸟日记之JSP1
JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它 是由Sun Microsyste ...
- linux创建守护进程
守护进程deamon 是一个后台进程,无需用户输入就能运行,用来在系统后台提供某种服务. 常见的守护进程有Wbe服务器.邮件服务器以及数据库服务器等等.守护进程不能控制终端,所以任何的输入和输出都需要 ...
- LCS最长公共子序列HDU1159
最近一直在学习算法,基本上都是在学习动态规划以及字符串.当然,两者交集最经典之一则是LCS问题. 首先LCS的问题基本上就是在字符串a,b之间找到最长的公共子序列,比如 YAOLONGBLOG 和 Y ...
- SGU 296.Sasha vs. Kate(贪心)
题意: 给出长度为n(<=1000)的一个数.输出删掉k个数字后的最大值. Solution: 简单贪心. s[i]代表数字s的第i位. 从前往后第一个满足s[i]>s[i-1]的位置,最 ...
- 第一天的CI笔记
1 CI不区分大小写2. http://xxx.com/index/[控制器名称]/[控制器里面方法的确名称]/[传入方法的参数 ]/ 3. 控制器及控制器类名称与文件名称一致, 继承 CI_Cont ...
- mysql datestamp坑
每次更改行数据,该行第一个datestamp如不赋值,会自动更新为当前时间.赋值还要注意用下new Date(time).updated_at要写在created_at前面...
- OnClose()和 OnDestroy()
OnClose()和 OnDestroy() 基于对话框的MFC程序,发现每次程序退出时,托盘的小图标不能自动消失,鼠标移上去之后才能消失,比较不爽. 后来发现我删除这个图标的代码是在自己重写的OnC ...
- [python]文本处理1.2
1.0初步完成了文本截取需要信息的处理 1.1 修复了格式所造成的遗漏字符 1.2 去除了遗漏字符中的多余字符 bug-文本test14 有遗漏字符 bug-修复的遗漏字符中含有\n 未被识别为换行符
- c++构造函数谁先执行的问题
看到网上一哥们的帖子 http://blog.csdn.net/maray/article/details/7761709 东西不多就转发了 1 #include <iostream> u ...