博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
问题 C: A+B Problem II
阅读量:7258 次
发布时间:2019-06-29

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

题目描述

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

A,B must be positive.

输入

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.

样例输入

21 2112233445566778899 998877665544332211

样例输出

Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
1 /*对于此题做法有两种: 2  其一,使2字符串的中的字符数字减去'0',逐个相加大于等于10的可以使本位减10,下一位自增1,后面的处理就非常简单了; 3  其二,便是读入字符串后先让各个字符减'0',一一对应存入整形数组中;之后再相加。 4     对于2种方法大致是相同的,都要从后面向前加,逢十进位,以及数组初始化均要初始为0,一边方便运算。 5  下面用法一,代码如下:*/ 6 #include
7 #include
8 9 int main( ) {10 int n, i,len1, len2, j, k, pi, t;11 char str1[1010], str2[1010];12 scanf("%d", &n);13 for(i = 1; i <= n; i++) {14 int a[1200] = {
0};15 int flag = 0;16 scanf("%s%s", str1, str2);//以字符串形式读入17 printf("Case %d:\n", i);18 len1 = strlen(str1);19 len2 = strlen(str2);20 printf("%s + %s = ", str1, str2);21 j = len1-1;22 k = len2-1;23 pi = 0;24 while(j >= 0 && k >= 0) { //开始相加25 if(a[pi] + (str1[j]-'0') + (str2[j]-'0') >= 10) { //相加后大于10的26 a[pi] = a[pi] + (str1[j] -'0')+(str2[k] - '0')-10;27 a[pi+1]++;28 }29 else30 a[pi] = a[pi] + (str1[j] - '0')+(str2[k]-'0');31 pi++; k--; j--;32 }33 if(j>=0) {34 for(t = j; t >= 0; t--) {35 a[pi] = a[pi]+(str1[t]-'0');36 pi ++;37 }38 }39 else if(k >= 0) {40 for(t = k; t >= 0; t--) {41 a[pi] = a[pi] + str2[t]-'0';42 pi++;43 }44 }45 else if(a[pi]!=0)//对于位数相同2个数加后最高位大于10的46 pi++;47 for(t = pi - 1; t >= 0; t--) {48 if(a[t]==0&&flag==0)49 continue;50 else51 {52 flag=1;53 printf("%d", a[t]);54 }55 }56 if(i!=n)//对于2组之间加空行的情况57 printf("\n");58 }59 return 0;60 }
View Code

 

转载于:https://www.cnblogs.com/tong69/p/5785457.html

你可能感兴趣的文章
Android API之android.os.Parcelable
查看>>
测试JdbcTemplate执行SQL语句和存储过程
查看>>
tableview 里面的 必须配套使用的方法
查看>>
ClassLoader工作机制
查看>>
子查询
查看>>
Android使用FFMpeg实现推送视频直播流到服务器
查看>>
(转)8 Tactics to Combat Imbalanced Classes in Your Machine Learning Dataset
查看>>
电信网络拓扑图自动布局之总线
查看>>
微信开放平台开发——网页微信扫码登录(OAuth2.0)
查看>>
DataList和Repeat无数据时提示暂无数据几种方法
查看>>
SharePoint is Flowers and Rainbows and Unicorns
查看>>
【转载】ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
系统视图和系统存储过程DDL语句
查看>>
C#温故而知新学习系列之XML编程—XmlSerializer类把复杂对象序列化为XML文档(六)...
查看>>
用C做的电子时钟程序
查看>>
符号引用(typeglob,别名)与全局变量的修改
查看>>
利用泛型实现通用的list和array转换
查看>>
打油诗 自嘲
查看>>
计划开始写redis的源码分析笔记
查看>>
java 空字条串空判断 效率
查看>>