博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hdoj 1002
阅读量:6271 次
发布时间:2019-06-22

本文共 1865 字,大约阅读时间需要 6 分钟。

问题描述

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

输入描述

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.

输出描述

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.

样例输入
2
1 2
112233445566778899 998877665544332211
样例输出
Case 1:
1 + 2 = 3
 
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

思路

简单的大数问题

代码

1 #include
2 #include
3 4 int main() 5 { 6 int n, j; 7 scanf("%d", &n); 8 for(j = 1; j <= n; j++) 9 {10 char ch1[1001], ch2[1001];11 scanf("%s", ch1);12 int s1 = strlen(ch1), i;13 int a[1001] = {
0}, b[1001] = {
0};14 for(i = s1-1; i >= 0; i--) { a[s1-i] = ch1[i] - '0'; }15 scanf("%s", ch2);16 int s2 = strlen(ch2);17 for(i = s2-1; i >= 0; i--) { b[s2-i] = ch2[i] - '0'; }18 printf("Case %d:\n%s + %s = ", j, ch1, ch2);19 int s = (s1>s2?s1:s2), temp=0;20 for(i = 1 ; i <= s; i++)21 {22 a[i] += b[i] + temp;23 temp = a[i] / 10;24 a[i] %= 10;25 }26 if(temp != 0) printf("%d",temp);27 for(i=s;i>0;i--) printf("%d",a[i]);28 if(j < n) printf("\n");29 printf("\n");30 }31 }

 

转载于:https://www.cnblogs.com/HackHarry/p/8289770.html

你可能感兴趣的文章
前端如何接收 websocket 发送过来的实时数据
查看>>
JavaWeb下载文件response
查看>>
Laravel的三种安装方法总结
查看>>
SpringMVC加载配置Properties文件的几种方式
查看>>
C#设计模式总结 C#设计模式(22)——访问者模式(Vistor Pattern) C#设计模式总结 .NET Core launch.json 简介 利用Bootstrap Paginat...
查看>>
java 项目相关 学习笔记
查看>>
numpy opencv matlab eigen SVD结果对比
查看>>
WPF获取某控件的位置,也就是偏移量
查看>>
Boost C++ 库 中文教程(全)
查看>>
solr查询优化(实践了一下效果比较明显)
查看>>
jdk目录详解及其使用方法
查看>>
说说自己对RESTful API的理解s
查看>>
通过layout实现可拖拽自动排序的UICollectionView
查看>>
服务器错误码
查看>>
javascript中的面向对象
查看>>
Splunk作为日志分析平台与Ossec进行联动
查看>>
yaffs文件系统
查看>>
Mysql存储过程
查看>>
NC营改增
查看>>
Lua
查看>>