九度[1037]-Powerful Calculator
题目描述:
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.
样例输入
20000000000000000
4000000000000000
样例输出
24000000000000000
16000000000000000
80000000000000000000000000000000
解题思路:
大整数计算,难倒是没什么难的,就是真的很繁琐。。。羡慕用Java的30行搞定。。
AC代码:
#include<cstdio>
#include <cstring>
const int maxn = 500;
struct bigInt{
int value[2*maxn];
int size;
int flag;
};
char str1[maxn], str2[maxn];
void init(bigInt &x){
for(int i = 0; i < 2*maxn; i++){
x.value[i] = 0;
}
x.size = 0;
x.flag = 1;
}
void toInt(char s[], bigInt &a){
int len = strlen(s);
a.size = 0;
for(int i = len - 1; i > 0; i--){
a.value[a.size++] = s[i] - '0';
}
if(s[0] == '-') a.flag = -1;
else {
a.flag = 1;
a.value[a.size++] = s[0] - '0';
}
}
bool isBigger(bigInt a, bigInt b){
if(a.size != b.size) return a.size > b.size;
else {
for(int i = 2*maxn-1; i < 2*maxn; i++){
if(a.value[i] != b.value[i]) return a.value[i] > b.value[i];
}
}
return true;
}
bigInt add(bigInt a, bigInt b){
bigInt addAns;
init(addAns);
for(int i = 0; i < maxn; i++){
int tmp = a.value[i] + b.value[i];
int pos = i;
addAns.value[i] += tmp;
while(addAns.value[i] >= 10){
addAns.value[pos+1] += addAns.value[pos]/10;
addAns.value[pos] %= 10;
pos++;
}
}
int index;
for(index = 2*maxn-1; index > 0; index--){
if(addAns.value[index] != 0) break;
}
addAns.size = index+1;
return addAns;
}
bigInt sub(bigInt a, bigInt b){
bigInt subAns;
init(subAns);
for(int i = 0; i < maxn; i++){
int tmp = a.value[i] - b.value[i];
int pos = i;
subAns.value[i] += tmp;
while(subAns.value[i] < 0){
subAns.value[pos+1] -= 1;
subAns.value[pos] += 10;
pos++;
}
}
int index;
for(index = 2*maxn-1; index > 0; index--){
if(subAns.value[index] != 0) break;
}
subAns.size = index+1;
return subAns;
}
bigInt mul(bigInt a, bigInt b){
bigInt mulAns;
init(mulAns);
if(a.flag != b.flag) mulAns.flag = -1;
for(int i = 0; i < a.size; i++){
for(int j = 0; j < b.size; j++){
int tmp = a.value[i] * b.value[j];
int pos = i+j;
mulAns.value[pos] += tmp;
while(mulAns.value[pos] >= 10) {
mulAns.value[pos+1] += mulAns.value[pos]/10;
mulAns.value[pos] %= 10;
pos++;
}
}
}
int index;
for(index = a.size + b.size; index > 0; index--){
if(mulAns.value[index] != 0) break;
}
mulAns.size = index+1;
return mulAns;
}
void print(bigInt x){
for(int i = x.size-1; i >= 0; i--){
printf("%d", x.value[i]);
if(i == 0) printf("\n");
}
}
int main(){
freopen("C:\\Users\\Administrator\\Desktop\\test.txt", "r", stdin);
while(scanf("%s%s", str1, str2) != EOF){
bigInt a, b;
init(a);
init(b);
toInt(str1, a);
toInt(str2, b);
bigInt addAns, subAns, mulAns;
if(a.flag == 1 && b.flag == 1){
addAns = add(a, b);
print(addAns);
if(isBigger(a, b)){
subAns = sub(a, b);
print(subAns);
}
else{
subAns = sub(b, a);
printf("-");
print(subAns);
}
}
else if(a.flag == -1 && b.flag == -1){
addAns = add(a, b);
printf("-");
print(addAns);
if(isBigger(a, b)){
subAns = sub(a, b);
printf("-");
print(subAns);
}
else{
subAns = sub(b, a);
print(subAns);
}
}
else if(a.flag == 1 && b.flag == -1){
if(isBigger(a, b)){
addAns = sub(a, b);
print(addAns);
}
else{
addAns = sub(b, a);
printf("-");
print(addAns);
}
subAns = add(a, b);
print(subAns);
}
else if(a.flag == -1 && b.flag == 1){
if(isBigger(a, b)){
addAns = sub(a, b);
printf("-");
print(addAns);
}
else{
addAns = sub(b, a);
print(addAns);
}
subAns = add(a, b);
printf("-");
print(subAns);
}
mulAns = mul(a, b);
print(mulAns);
}
fclose(stdin);
return 0;
}