UVa 424 Integer Inquiry
之前杭电上也做过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的更多相关文章
- UVa 424 Integer Inquiry 【大数相加】
解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当 ...
- 424 - Integer Inquiry
Integer Inquiry One of the first users of BIT's new supercomputer was Chip Diller. He extended his ...
- hdu acm-1047 Integer Inquiry(大数相加)
Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Integer Inquiry【大数的加法举例】
Integer Inquiry Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27730 Accepted: 10764 ...
- hdu1047 Integer Inquiry
/* Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Integer Inquiry
Integer Inquiry Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Sub ...
- hdu 1047 Integer Inquiry
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1047 Integer Inquiry Description One of the first use ...
- Poj 1503 Integer Inquiry
1.链接地址: http://poj.org/problem?id=1503 2.题目: Integer Inquiry Time Limit: 1000MS Memory Limit: 1000 ...
- hdoj 1047 Integer Inquiry
Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- oom日志查看
这通常会触发 Linux 内核里的 Out of Memory (OOM) killer,OOM killer 会杀掉某个进程以腾出内存留给系统用,不致于让系统立刻崩溃.如果检查相关的日志文件(/va ...
- poj 2723
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7295 Accepted: 2778 Des ...
- 深入了解linux下的last命令及其数据源
http://www.9usb.net/200902/linux-last.html http://blog.csdn.net/chaofanwei/article/details/11826567
- js对象小结
前奏 对象是js的基本数据类型,准确来说除了字符串,数字,boolean值,null与undifine之外,js中的值都是对象.js中的对象是一种复合值,他将很多值(原始值或其他对象)聚合在一起,可以 ...
- springmvc的mvc:view-controller标签
1.重定向 <mvc:view-controller path="/" view-name="redirect:/admin/index"/> 即如 ...
- 李洪强漫谈iOS开发[C语言-043]-练习
李洪强漫谈iOS开发[C语言-043]-行李重量计算价格
- JSTL Tag学习笔记之<fn: />
在JSTL中也提供了一些标准的函数,但是几乎都是和操作字符串相关的函数,如果需要使用这类函数的话,应该引入下面的taglib: <%@ taglib prefix="fn" ...
- iOS xcode设置
Xcode build search paths c/c++ 头文件引用问题include <> 引用编译器的类库路径下的头文件include “” 引用工程目录的相对路径的头文件 inc ...
- linux查询cpu核心数
linux怎么查询cpu核心数 1.查看逻辑CPU个数: #cat /proc/cpuinfo |grep "processor"|sort -u|wc -l24 2.由于有超线程 ...
- Oracle 全文索引相关命令
--用sys用户登录,解锁ctxsys用户alter user ctxsys account unlock; --授权给对应用户grant execute on ctx_ddl to yw; --用y ...