九度OJ 1037 Powerful Calculator
来源:互联网 发布:中印军力 知乎 编辑:程序博客网 时间:2024/05/21 17:49
- 题目描述:
Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed and appeared in future market shortly. SJTU now invites you attending such amazing research and development work.
In most business applications, the top three useful calculation operators are Addition (+), Subtraction (-) and Multiplication (×) between two given integers. Normally, you may think it is just a piece of cake. However, since some integers for calculation in business application may be very big, such as the GDP of the whole world, the calculator becomes harder to develop.
For example, if we have two integers 20 000 000 000 000 000 and 4 000 000 000 000 000, the exact results of addition, subtraction and multiplication are:
20000000000000000 + 4000000000000000 = 24 000 000 000 000 000
20000000000000000 - 4000000000000000 = 16 000 000 000 000 000
20000000000000000 × 4000000000000000 = 80 000 000 000 000 000 000 000 000 000 000
Note: SJTU prefers the exact format of the results rather than the float format or scientific remark format. For instance, we need "24000000000000000" rather than 2.4×10^16.
As a programmer in SJTU, your current task is to develop a program to obtain the exact results of the addition (a + b), subtraction (a - b) and multiplication (a × b) between two given integers a and b.
- 输入:
Each case consists of two separate lines where the first line gives the integer a and the second gives b (|a| <10^400 and |b| < 10^400).
- 输出:
For each case, output three separate lines showing the exact results of addition (a + b), subtraction (a - b) and multiplication (a × b) of that case, one result per lines.
- 样例输入:
200000000000000004000000000000000
- 样例输出:
240000000000000001600000000000000080000000000000000000000000000000
#include <stdio.h>#include <string.h>struct BigInt{int digit[1000];int size;int sign; //正数为0,负数为1}a,b,c;void init(BigInt &a) // 初始化大数{for(int i = 0; i < 1000; i++)a.digit[i] = 0;a.size = 0;a.sign = -1;}bool isBig(BigInt a, BigInt b) //比较a与b大小{if(a.size > b.size)return true;else if(a.size < b.size)return false;else{for(int i = a.size - 1; i >= 0; i--){if(a.digit[i] > b.digit[i])return true;else if(a.digit[i] < b.digit[i])return false;}}return true;}void string_to_int(char s[], BigInt &a) //字符串转为高精度整数{for(int i = strlen(s) - 1; i >= 0; i--)if(s[i] >= '0' && s[i] <= '9')a.digit[a.size++] = s[i] - '0';if(s[0] == '-')a.sign = 1;elsea.sign = 0;}BigInt Add(BigInt a, BigInt b) // 加法,用于正+正,负+负,正-负,负-正{BigInt ret;init(ret);int carry = 0;int bound = a.size > b.size ? a.size : b.size;for(int i = 0; i < bound; i++){int tmp = a.digit[i] + b.digit[i] + carry;ret.digit[ret.size++] = tmp % 10;carry = tmp / 10;}if(carry != 0)ret.digit[ret.size++] = carry;return ret;}BigInt Sub(BigInt a, BigInt b) // 减法,a > b,用于正-正,负-负,正+负,负+正{BigInt ret;init(ret);for(int i = 0; i < a.size; i++){if(a.digit[i] - b.digit[i] >= 0)ret.digit[i] = a.digit[i] - b.digit[i];else{a.digit[i+1]--;ret.digit[i] = a.digit[i] + 10 - b.digit[i];}}int j;for(j = a.size - 1; j >= 0; j--)if(ret.digit[j] != 0)break;ret.size = j + 1;return ret;}BigInt Mul(BigInt a, BigInt b) // 乘法{BigInt ret;init(ret);for(int i = 0; i < a.size; i++){for(int j = 0; j < b.size; j++){int pos = i + j;ret.digit[pos] += (a.digit[i] * b.digit[j]);while(ret.digit[pos] >= 10){ret.digit[pos+1] += ret.digit[pos] / 10;ret.digit[pos] %= 10;pos++;}}}int k;for(k = 999; k >= 0; k--)if(ret.digit[k] != 0)break;ret.size = k + 1;return ret;}int main(){//freopen("Test.txt","r",stdin);char s1[500], s2[500];while(scanf("%s%s",s1,s2) != EOF){int i;init(a);init(b);string_to_int(s1,a);string_to_int(s2,b);//printf("%d %d\n",a.sign,b.sign);BigInt add_ret;init(add_ret);BigInt sub_ret;init(sub_ret);BigInt mul_ret;init(mul_ret);mul_ret = Mul(a,b);if(a.sign == 0 && b.sign == 0){add_ret = Add(a,b);add_ret.sign = 0;if(isBig(a,b) == true){sub_ret = Sub(a,b);sub_ret.sign = 0;}else{sub_ret = Sub(b,a);sub_ret.sign = 1;}mul_ret.sign = 0;}else if(a.sign == 0 && b.sign == 1){sub_ret = Add(a,b);sub_ret.sign = 0;if(isBig(a,b) == true){add_ret = Sub(a,b);add_ret.sign = 0;}else{add_ret = Sub(b,a);add_ret.sign = 1;}mul_ret.sign = 1;}else if(a.sign == 1 && b.sign == 0){sub_ret = Add(a,b);sub_ret.sign = 1;if(isBig(a,b) == true){add_ret = Sub(a,b);add_ret.sign = 1;}else{add_ret = Sub(b,a);add_ret.sign = 0;}mul_ret.sign = 1;}else if(a.sign == 1 && b.sign == 1){add_ret = Add(a,b);add_ret.sign = 1;if(isBig(a,b) == true){sub_ret = Sub(a,b);sub_ret.sign = 1;}else{sub_ret = Sub(b,a);sub_ret.sign = 0;}mul_ret.sign = 0;}// 输出和、差、积if(add_ret.sign == 1)printf("-");if(add_ret.size == 0)printf("0\n");else{for(i = add_ret.size - 1; i >= 0; i--)printf("%d",add_ret.digit[i]);printf("\n");}if(sub_ret.sign == 1)printf("-");if(sub_ret.size == 0)printf("0\n");else{for(i = sub_ret.size - 1; i >= 0; i--)printf("%d",sub_ret.digit[i]);printf("\n");}if(mul_ret.sign == 1)printf("-");if(mul_ret.size == 0)printf("0\n");else{for(i = mul_ret.size - 1; i >= 0; i--)printf("%d",mul_ret.digit[i]);printf("\n");}//printf("\n");}return 0;}
- 九度 oj 题目1037:Powerful Calculator
- 九度OJ 1037 Powerful Calculator
- 九度oj-1037-Powerful Calculator
- 九度 oj 题目1037:Powerful Calculator
- 九度oj 1037 Powerful Calculator 高精度算法
- 九度[1037]-Powerful Calculator
- 【九度】题目1037:Powerful Calculator
- 九度OJ 1037:Powerful Calculator(强大的计算器) (大整数运算)
- 题目1037:Powerful Calculator
- 题目1037:Powerful Calculator
- 题目1037:Powerful Calculator
- 题目1037:Powerful Calculator
- 题目1037:Powerful Calculator
- 题目1037:Powerful Calculator
- OJ_1037 Powerful Calculator
- 1037.Powerful Calculator
- 九度oj 1037
- 上交考研复试07年Powerful Calculator
- only final is permitted
- js中settimeout 方法的使用,加参数实例
- java之用volatile和不用volatile的区别
- C++中的各种小细节(三)
- UT2416 wince 6.0 调试LCD35 触摸驱动参考
- 九度OJ 1037 Powerful Calculator
- acegi security实践教程—form认证
- 控制不同进制的输出
- 15-hibernate实战 多对多对象关系模型映射(学生与老师)
- HDU4502:吉哥系列故事——临时工计划(DP)
- Android初学者之SharedPreferences
- SPFA算法之最短路。
- 每日总结
- BCM芯片FP原理及相关SDK数据结构介绍