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 ...
随机推荐
- PHP常用数组函数
一.数组操作的基本函数 数组的键名和值 array_values($arr); 获得数组的值 array_keys($arr); 获得数组的键名 array_flip($arr); 数组中的 ...
- tomcat gc问题总结
Java内存泄露监控工具:JVM监控工具介绍 http://developer.51cto.com/art/201203/321431.htm 关于施用full gc频繁的分析及解决 http:/ ...
- ProfessionalKnowledgeArchitecture
- IE8,IE9,IE10绿色版,以及ColorPix
对于开发者而言IETest是一坨屎. 但是我们终于迎来的蛋糕和火腿,今天无意间发现了竟然有绿色版的浏览器版本,IE9,IE10已经下到本地,IE8网传是有的,但是没有找到合适的版本.但是足够了,本机使 ...
- 学习Swift -- 构造器(上)
构造器(上) 构造过程是为了使用某个类.结构体或枚举类型的实例而进行的准备过程.这个过程包含了为实例中的每个存储型属性设置初始值和为其执行必要的准备和初始化任务. 构造过程是通过定义构造器(Initi ...
- Python/Keras如何将给定的数据集打乱
给定数据集data,数据集对应的标签label index = [i for i in range(len(data))] random.shuffle(index) data = data[inde ...
- Hadoop 学习笔记 (十) MapReduce实现排序 全局变量
一些疑问:1 全排序的话,最后的应该sortJob.setNumReduceTasks(1);2 如果多个reduce task都去修改 一个静态的 IntWritable ,IntWritable会 ...
- SAAS相关技术要点
这篇文章本来是我们开发组内部用的一个小文档.因为我们公司以前没有做SAAS的经验,就成立了一个小组做一做这方面的技术前探,我是成员之一.这篇文档想从宏观的层面把开发一个SAAS应用所要用到的技术点稍微 ...
- iPhone手机VPN设置
如果iPhone,iPad游戏或软件服务器在国外不能用,就需要设置VPN了. 如果是为了解除公司上网策略限制,或者为了上Google,Facebook,都可以通过设置VPN实现. 要使用VPN需要到V ...
- Android listView scroll 恢复滚动位置
相信大家尝试过许多方法恢复滚动位置,本人也找了许多方法,唯有这个方法好用,下面把代码贴出来 声明两个变量 private int mPosition; private int lvChildTop; ...