OJ_1037 Powerful Calculator
来源:互联网 发布:手机淘宝不能图片搜索 编辑:程序博客网 时间:2024/05/14 03:26
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <algorithm>using std::max; const int MAXLEN = (400 + 2)*2; char A[MAXLEN];char B[MAXLEN];char C[MAXLEN]; int arrA[MAXLEN];int arrB[MAXLEN];int arrC[MAXLEN];int arrTmp[MAXLEN]; //int max(int a, int b)//{// return a > b ? a : b;//} void StrToBigInt(char a[], int arr[], int& len, bool& positive){ int i, j, k; if(a[0]=='-') { positive = false; i=1; } else { positive = true; i=0; } while(a[i]) { arr[i] = a[i] - '0'; i++; } len = max(i, 1); j=0;k=len-1; while(j<k) { std::swap(arr[j], arr[k]); j++; k--; }} void BigIntToStr(int arr[], int len, bool positive, char a[]){ int i; if(positive) { i=0; } else { a[0]='-'; i=1; } int j = len - 1; while(j>=0) { a[i] = '0' + arr[j]; j--; i++; } a[i] = '\0';} int BigCmp(int a[], int b[], int lenA, int lenB){ if(lenA<lenB)return -1; if(lenA>lenB) return 1; int i; for(i=lenA-1;i>=0&&a[i]==b[i];i--); if(i>=0) { if(a[i]<b[i]) { return -1; } else { return 1; } } else { return 0; }} int BigAdd(int a[], int b[], int c[], int lenA, int lenB, int M){ int i; int advance = 0; int maxLen = max(lenA, lenB)+1; for(i=lenA;i<=maxLen;i++)a[i]= 0; for(i=lenB;i<=maxLen;i++)b[i]= 0; for(i=0;i<maxLen;i++) { int tmp = a[i] + b[i] + advance; if(tmp >= M) { advance = 1; c[i] = tmp - M; } else { advance = 0; c[i] = tmp; } } i--; while(i>=0 &&!c[i])i--; return max(i+1, 1);} int BigSub(int a[], int b[], int c[], int lenA, int lenB, int M){ int i; int additional = 0; int maxLen = max(lenA, lenB); for(i=lenA;i<=maxLen;i++)a[i]= 0; for(i=lenB;i<=maxLen;i++)b[i]= 0; for(i=0;i<maxLen;i++) { int tmp = a[i] + additional - b[i]; if(tmp>=0) { additional = 0; c[i] = tmp; } else { additional = -1; c[i] = tmp + M; } } i--; while(i>=0 &&!c[i])i--; return max(i+1, 1);} int BigMult(int a[], int b, int n, int c[], int lenA, int M){ int i; int advance = 0; a[lenA]=0; for(i=0;i<=lenA;i++) { int tmp = a[i] * b + advance; advance = tmp / M; c[i] = tmp % M; } for(i=lenA;i>=0;i--) { c[i+n] = c[i]; } for(i=0;i<n;i++)c[i]=0; i=lenA + n; while(i>=0 &&!c[i])i--; return max(i+1, 1);} int BigMult(int a[], int b[], int c[], int lenA, int lenB, int M){ int i; int lenC=1; c[0] = 0; for(i=0;i<lenB;i++) { int lenTmp = BigMult(a, b[i], i, arrTmp, lenA, M); lenC = BigAdd(c, arrTmp, c, lenC, lenTmp, M); } return lenC;} int main(){ while(scanf("%s%s", &A, &B)!=EOF) { int lenA; bool aPositive; int lenB; bool bPositive; int lenC; bool cPositive; StrToBigInt(A, arrA, lenA, aPositive); StrToBigInt(B, arrB, lenB, bPositive); // Add if(aPositive && bPositive) { cPositive = aPositive; lenC = BigAdd(arrA, arrB, arrC, lenA, lenB, 10); } else { int cmp = BigCmp(arrA, arrB, lenA, lenB); switch(cmp) { case 0: arrC[0] = 0; lenC = 1; break; case 1: cPositive = aPositive; lenC = BigSub(arrA, arrB, arrC, lenA, lenB, 10); break; case -1: cPositive = bPositive; lenC = BigSub(arrB, arrA, arrC, lenB, lenA, 10); break; } } BigIntToStr(arrC, lenC, cPositive, C); printf("%s\n", C); // Subsctract if(lenB==1&&arrB[0]==0) { bPositive = true; } else { bPositive = !bPositive; } if(aPositive && bPositive) { cPositive = aPositive; lenC = BigAdd(arrA, arrB, arrC, lenA, lenB, 10); } else { int cmp = BigCmp(arrA, arrB, lenA, lenB); switch(cmp) { case 0: arrC[0] = 0; lenC = 1; break; case 1: cPositive = aPositive; lenC = BigSub(arrA, arrB, arrC, lenA, lenB, 10); break; case -1: cPositive = bPositive; lenC = BigSub(arrB, arrA, arrC, lenB, lenA, 10); break; } } BigIntToStr(arrC, lenC, cPositive, C); printf("%s\n", C); if(lenB==1&&arrB[0]==0) { bPositive = true; } else { bPositive = !bPositive; } // Multiple if( (lenA==1 && arrA[0]==0 )|| (lenB==1 && arrB[0]==0)) { cPositive = true; } else { cPositive = (aPositive == bPositive); } lenC = BigMult(arrA, arrB, arrC, lenA, lenB, 10); BigIntToStr(arrC, lenC, cPositive, C); printf("%s\n", C); } return 0;}
大数加减乘,没做
- 题目描述:
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
- OJ_1037 Powerful Calculator
- 题目1037:Powerful Calculator
- 题目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
- 九度OJ 1037 Powerful Calculator
- 【九度】题目1037:Powerful Calculator
- 上交考研复试07年Powerful Calculator
- 九度oj-1037-Powerful Calculator
- Powerful Calculator(上海交大上机考试)
- 九度 oj 题目1037:Powerful Calculator
- 九度oj 1037 Powerful Calculator 高精度算法
- 九度OJ 1037:Powerful Calculator(强大的计算器) (大整数运算)
- error LNK2005
- 如何避免僵尸进程
- Leetcode Maximum Depth of Binary Tree
- Spring MVC 与 itextpdf 集成实现PDF签名的maven包依赖冲突
- 指定Action、Category调用系统Activity实例-点击按钮,返回Home桌面
- OJ_1037 Powerful Calculator
- 云南老人毒死2个月大外孙 称为报复婴儿父母
- OJ_1038
- Remove Duplicates from Sorted Array II
- HDU 1403 Longest Common Substring
- title_test-1188220387
- title_test69996405
- title_test-1131689306
- title_test-434657722