1C - A + B Problem II
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3 Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110 // too young and didn't notice that
// "you should not process them by using 32-bit integer"
// and "Output a blank line between two test cases".
#include<stdio.h>
int main()
{
int a, b, t, i;
scanf("%d", &t);
for(i=;i<=t;i++)
{
scanf("%d %d", &a, &b);
printf("Case %d:\n", i);
printf("%d + %d = %d\n", a, b, a+b);
printf("\n");
}
return ;
}
Wrong Answer
// long int is also 32-bit integer.
代码省略
// 不用判断哪个数较长,只要记录答案的长度. 三个数组都要初始化为0!!!
#include<stdio.h>
#include<string.h> int reverse_add(int *a, int *b, int *c, int al, int bl)
{
int i, sum=, k, max;
if(al<bl) max=bl;
else max=al;
k=;
for(i=; i<max; i++)
{
*(c+i)=(*(a+i)+*(b+i)+k)%;
k=(*(a+i)+*(b+i)+k)/;
}
if(k!=)
{
*(c+i)=;
return max+;
}
else return max;
} int main()
{
char s1[], s2[];
int t, k, i, length_a, length_b, rmax;
scanf("%d", &t);
for(k=;k<=t;k++)
{
int a[]={}, b[]={}, c[]={}; /* 三个数组都要初始化 */
scanf("%s %s", s1, s2);
length_a=strlen(s1); length_b=strlen(s2);
for(i=; i<length_a; i++) a[i]=s1[length_a--i]-'';
for(i=; i<length_b; i++) b[i]=s2[length_b--i]-'';
rmax=reverse_add(a, b, c, length_a, length_b);
printf("Case %d:\n", k);
printf("%s + %s = ", s1, s2);
for(i=; i<rmax; i++) printf("%d", c[rmax--i]);
printf("\n");
if(t>&&k<t) printf("\n");
}
return ;
}
AC
1C - A + B Problem II的更多相关文章
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- nyoj 623 A*B Problem II(矩阵)
A*B Problem II 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 ACM的C++同学有好多作业要做,最头痛莫过于线性代数了,因为每次做到矩阵相乘的时候,大 ...
- HDU 1002 A + B Problem II
A + B Problem II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted ...
- A + B Problem II
之前总是在查阅别人的文档,看着其他人的博客,自己心里总有一份冲动,想记录一下自己学习的经历.学习算法有一段时间了,于是想从算法开始自己的博客生涯O(∩_∩)O~~ 今天在网上看了一道大数相加(高精度) ...
- nyoj 103 A + B problem II
点击打开链接 A+B Problem II 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 I have a very simple problem for you. G ...
- hdu 1023 Train Problem II
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...
- HDU1002 -A + B Problem II(大数a+b)
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过
杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...
- hdoj 1002 A + B Problem II
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- Hibernate 再接触 集合映射
不太重要 List 用于排序 Map key一般是user的某个字段(多半是主键 integer) package com.bjsxt.hibernate; import java.util.Has ...
- oracle理解和导入导出
搞过sql server的程序员很难理解oracle的表空间.我在这里简单说一下吧, oracle中的表空间就相当于sql server中的实例,用户就相当于sql server中的库. 所以在ora ...
- UIApplication 的学习
1.0 URL 的组成 == 协议头://主机名/路径 从iOS7 开始,系统提供了两种管理状态栏的方式,默认交给控制器去管理 2.0 旋转事件----> UIApplication --- ...
- Oracle 导入大量数据
环境是这样的: 需要导入大量数据到Oracle,目前Oracle已建立索引和触发器了,导入的数据是树型结构,需要关联. 采用的方法是: 删除以前数据库的索引和触发器,用OracleBulkCopy批量 ...
- python垃圾回收机制(转)
Python的GC模块主要运用了“引用计数”(reference counting)来跟踪和回收垃圾.在引用计数的基础上,还可以通过“标记-清除”(mark and sweep)解决容器对象可能产生的 ...
- json数组转java对象
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>ison-lib</art ...
- Cacti日志时区问题
cacti默认是以美国的时间为准的,监测的适合要纠正到UTC+8的时区. 打开vi /home/cacti/include/config.php 文件,在里面加入一行 date_default_tim ...
- 解题(LevenshteinInstance--Levenshtein距离)
题目描述 Levenshtein 距离,又称编辑距离,指的是两个字符串之间,由一个转换成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符.编辑距离 ...
- django 导入数据库
python manage .py makemigrations appname python manage.py migrate
- python学习day4 数据类型 if语句
1.变量的内存管理 cpython解释器垃圾回收机制 什么是垃圾,当一个值身上没有绑定变量名时,(该值的引用计数=0时)就是一个垃圾 age=18 #18的引用计数=1 x=age #18的引用计数 ...