Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
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
1#include <stdio.h>
2
3int main()
4{
5 int num, i, k; //num: The number of lines, i & j: loop counter
6 int lengtha = -1, lengthb = -1, lengthl, lengths; //The length of summand, the length of addend, the longer one, the shorter one
7 char a[1000], b[1000]; //summand, addend
8 char* c = new char[1000]; //result
9 char temp;
10 int rlt = 0, adrlt = 0; //result of a sigle digit, to carry to the next digit
11
12 scanf("%d", &num); //input the first line, the number of input lines
13 getchar();
14
15 for(k = 0; k < num; k++)
16 {
17 /**//* initialization */
18 lengtha = 0;
19 lengthb = 0;
20 for (i = 0; i < 1000; i++)
21 {
22 a[i] = '0';
23 b[i] = '0';
24 c[i] = '0';
25 }
26
27 /**//* get the input value */
28 for(i = 0; (temp = getchar()) != ' ' && i < 1000; i++) //summand until space
29 {
30 a[i] = temp;
31 lengtha++;
32 }
33 for(i = 0; (temp = getchar()) != '\n' && i < 1000; i++) //addend until '\n'
34 {
35 b[i] = temp;
36 lengthb++;
37 }
38
39 /**//* print the result format */
40 printf("Case %d:\n", k + 1);
41 for (i = 0; i < lengtha; i++)
42 {
43 printf("%c", a[i]);
44 }
45 printf(" + ");
46 for (i = 0; i < lengthb; i++)
47 {
48 printf("%c", b[i]);
49 }
50 printf(" = ");
51
52 /**//* find the longer one between summand and addend */
53 if(lengtha > lengthb)
54 {
55 lengths = lengthb;
56 lengthl = lengtha;
57 c = a;
58 }
59 else
60 {
61 lengths = lengtha;
62 lengthl = lengthb;
63 c = b;
64 }
65
66 /**//* add process */
67 for(i = 0; i <= lengths; i++)
68 {
69 rlt = 0;
70 rlt = (a[lengtha - i] - '0') + (b[lengthb - i] - '0') + adrlt;
71 if(rlt > 9)
72 {
73 rlt = rlt - 10; //if any result of digit needs to carry to the next digit
74 adrlt = 1;
75 }
76 else
77 adrlt = 0;
78 c[lengthl - i] = rlt + '0';
79 }
80 /**//* carrying for the highest digit of the shorter one */
81 if(adrlt == 1)
82 {
83 c[lengthl - lengths - 1] = (c[lengthl - lengths - 1] - '0') + 1 + '0';
84 }
85
86 /**//* print the result */
87 for (i = 0; i < lengthl; i++)
88 {
89 printf("%c", c[i]);
90 }
91 if(k != num - 1)
92 printf("\n\n");
93 else
94 printf("\n");
95 }
96}
Summary:
Solving this problem makes me absolutely overjoyed. You konw. for me, a newbie, to figure out such a nut witout any referance. Ok, let's get to business.
Besides the puzzling logic, the input-output format is still the key point in this problem. How to read and store the big decimal in a character array by digit? How to make the reading-terminal-condition?
Another gist is that how to make the additon when summand and addend don't have the same numbers of digits. So take the lengths of them respectively into account is very important.
When taking the addition, begin the whole process from the lowest digit, so you're suppose to iterate the arrays in a reverse order. At this time, the longer length and shorter one will be used for the for condition.
About the convertion of data types:
Int and char are actually of the same data type which is represented by a integer(emu as well). So when you use char a = (int) b, or int a = (char) b; a actually gets the ASCII value of b, not b itselt. That is to say will get 49 for (int)'1'.
To make '2' to 2,
int a = 1;
char b;
b = '0' + a;
or
char a = '1';
int b;
b = a - '0';
分享到:
相关推荐
std :: decimal 这是建议的C ++ std :: decimal的实现,其中包括修订版。 它建立在完成所有艰苦工作的上。意图过去,我已经成功地使用了,但是我想构建一个依靠模板而不是宏来生成所需的大量运算符的现代版本。测验...
winform小练习: 两decimal数求和
use decimal_rs :: Decimal; let n1: Decimal = "123" . parse (). unwrap (); let n2: Decimal = "456" . parse (). unwrap (); let result = n1 + n2; assert_eq! (result. to_string (), "579" ); 要从Rust基本...
js-big-decimal.min.js, 用于处理Js大整数等
用法要构建小数,请使用Decimal :use decimal_rs:: Decimal;let n1: Decimal="123" .parse ().unwrap ();let n2: Decimal="456" .parse ().unwrap ();let result= n1+ n2;assert_eq! (result.to_string (),"579" );...
johnnydecimal-index-generator Johnny Decimal Index Generator Web应用程序的代码
decimal的底数是10,是十进制的。有4个Int32整数组成。低low、中middle、高high、【符号位和小数位数】组成的一个整数。decimal的底数是10。【注意decimal的根脚与double的根脚不同。decimal是10,double和float是2...
的轻量版本, 的任意精度 Decimal 类型。 这个库是最新的库家族: 、 、 和decimal.js-light 。 该 API 或多或少是decimal.js的 API 的一个子集。 这个库和decimal.js之间的差异缩小后的decimal.js 的大小:32.1 KB...
此 Simulink 模型显示了一个简单的代码,可将十进制数转换为最多 8 位(十进制到 255 位)的二进制数。 然而,它很容易配置,例如,如果需要 9 位,只需更改 Matlab 功能块中的 2 行代码,如下所示: 1) a=零(1,8)...
def decimal_to_binary(n): binary = "" while n > 0: binary = str(n % 2) + binary n = n // 2 return binary decimal = int(input("请输入一个十进制数:")) binary = decimal_to_binary(decimal) print(f...
MySQL中存在float,double等非标准数据类型,也有decimal这种标准数据类型。 其区别在于,float,double等非标准类型,在DB中保存的是近似值,而Decimal则以字符串的形式保存数值。 float,double类型是可以存浮点数...
资源来自pypi官网。 资源全名:decimal_monkeypatch-0.4.3-py2-none-any.whl
在操作数据库时,需要将字符串转换成decimal类型。 代码如下: select cast('0.12' as decimal(18,2)); select convert(decimal(18,2), '0.12'); 当需要将科学计数法的数字字符串转换成decimal时,这2种写法都...
pg十进制PostgreSQL用于decimal64和decimal128。 Decimal64将具有16个精度数字,而decimal128将具有34。34很大。 这项工作的灵感来自于Pavel Stehule的pgDecimal软件包。 有关原始作者的文档,请参见README.pavel。 ...
decimal.js 实现了对任意精度的十进制进行计算的 JavaScript 库。 标签:decimal
decimal 模块实现了定点和浮点算术运算符,使用的是大多数人所熟悉的模型,而不是程序员熟悉的模型,即大多数计算机硬件实现的 IEEE 浮点数运算。这篇文章主要介绍了python里的decimal类型转换,需要的朋友可以参考下
前端项目-decimal.js,javascript的任意精度十进制类型。
在您的mix.exs文件中添加Decimal作为依赖mix.exs : def deps do [{ :decimal , " ~> 2.0 " }] end 接下来,在您的shell中运行mix deps.get以获取并编译Decimal 。 使用iex -S mix启动交互式Elixir Shell: iex ...
Decimal BASIC FOR WINDOWS