之前杭电上也做过a + b的高精度的题,不过这道题的区别是有多组数据。

之前做的时候开了3个字符数组a,b,c,在计算的时候还要比较a,b长度,短的那个还要加'0',还设置了一个add来存放进位。

现在看来这种算法确实很繁琐。

而这次只用了两个字符数组,一个放加数,一个放和。

相比之前程序更短小了,而且可读性也提高了。

果然办法都是逼出来的。

没有了add,在判断进位的时候就看那一位的ASCII码是否>'9',然后进位。

尤其需要注意的一点是可能会出现连续进位的情况,比如99999 + 1。解决的办法就是用循环来控制。

 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

AC代码:

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
char Linteger[maxn];
char Resault[maxn]; void Reverse(char s[], int l); int main(void)
{
#ifdef LOCAL
freopen("424in.txt", "r", stdin);
#endif int i;
memset(Resault, '', sizeof(Resault));//将结果全部初始化为'0'
while(gets(Linteger) && Linteger[] != '')
{
int l = strlen(Linteger);
Reverse(Linteger, l);
for(i = ; i < l; ++i)
{
Resault[i] = Resault[i] + Linteger[i] - '';
if(Resault[i] > '')
{
int j = i;
while(Resault[j] > '')//考虑连续进位的情况
{
Resault[j] -= ;
++j;
++Resault[j];
}
}
}
}
for(i = ; i >= ; --i)//输出时忽略前导0
if(Resault[i] != '')
break;
for(; i >= ; --i)
cout << Resault[i];
cout << endl;
return ;
}
void Reverse(char s[], int l)//用来反转数组,从个位开始加起
{
int i;
char t;
for(i = ; i < l / ; ++i)
{
t = s[i];
s[i] = s[l - i -];
s[l - i -] = t;
}
}

代码君

UVa 424 Integer Inquiry的更多相关文章

  1. UVa 424 Integer Inquiry 【大数相加】

    解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当 ...

  2. 424 - Integer Inquiry

     Integer Inquiry  One of the first users of BIT's new supercomputer was Chip Diller. He extended his ...

  3. hdu acm-1047 Integer Inquiry(大数相加)

    Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. Integer Inquiry【大数的加法举例】

    Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27730   Accepted: 10764 ...

  5. hdu1047 Integer Inquiry

    /* Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  6. Integer Inquiry

    Integer Inquiry Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Sub ...

  7. hdu 1047 Integer Inquiry

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1047 Integer Inquiry Description One of the first use ...

  8. Poj 1503 Integer Inquiry

    1.链接地址: http://poj.org/problem?id=1503 2.题目: Integer Inquiry Time Limit: 1000MS   Memory Limit: 1000 ...

  9. hdoj 1047 Integer Inquiry

    Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. redis 数据库维护之 key 大小获取

    获得 redis key 大小 redis 用过一段时间后,发现一个KEY每天需更新值,但总是更新不全,故此为了定位问题,整理此脚本,辅助监控一下 写了两个脚本 注意:需要提前从 https://gi ...

  2. ASP.NET制作一个简单的等待窗口

    前一阵做一个项目,在处理报表的时候时间偏长,客户提出要做出一个等待窗口提示用户等待(页面太久没反映,用户还以为死了呢).在分析这一需求之后,觉得如果要实现像winform应用中的processbar太 ...

  3. java输出流实现文件下载

    //导出Excel try { HSSFWorkbook wb = carService.export(list); //调用service方法~! response.setContentType(& ...

  4. POJ 2151 Check the difficulty of problems (概率dp)

    题意:给出m.t.n,接着给出t行m列,表示第i个队伍解决第j题的概率. 现在让你求:每个队伍都至少解出1题,且解出题目最多的队伍至少要解出n道题的概率是多少? 思路:求补集. 即所有队伍都解出题目的 ...

  5. XCODE快捷键个人总结

    1.在方法名上用CMD+左键 可以查看完整的方法名

  6. kafka配置

    官网:http://kafka.apache.org/ 主要有3种安装方式: 1. 单机单broker 2. 单机多broker 3. 多机多broker 1. wget http://mirror. ...

  7. 浅说Java中的反射机制(一)

    在学习传智播客李勇老师的JDBC系列时,会出现反射的概念,由于又是第一次见,不免感到陌生.所以再次在博客园找到一篇文章,先记录如下: 引用自java中的反射机制,作者bingoideas.(()为我手 ...

  8. excel设置下拉菜单,并且不同值会显示不同颜色

    工作中常常要用的excel,每次都会有新的需求,然后不会,然后百度,然后过段时间可能就又忘了,于是就想说,自己记录下来~~~因为自己用的都是2010,其实哪个版本都差不多,都是应该可以找到相应的按钮滴 ...

  9. lintcode:删除链表中指定元素

    题目 删除链表中等于给定值val的所有节点. 样例 给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1-> ...

  10. yarn介绍

    hadoop 1.0 mapreduce过程 主要问题: JobTracker 是 Map-reduce 的集中处理点,存在单点故障. JobTracker 完成了太多的任务,造成了过多的资源消耗,当 ...