九度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;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 注册微信公众号邮箱激活不了怎么办 不是自己申请的qq号忘密码怎么办 联通宽带拨号账号密码忘记了怎么办 忘了路由器的用户名和密码怎么办 宽带连接用户名和密码忘了怎么办 江西银行网银用户名忘记了怎么办 江西银行网银密码忘了怎么办 广发信用卡网银密码忘了怎么办 刚注册的淘宝账号买不了东西怎么办 隐藏后的wif不知道用户名怎么办 脊柱侧弯术后钢钉断了一根怎么办 对法院执行裁定申请复议过期怎么办 自己家店名被别人注册了商标怎么办 有人去工商局投诉我公司了怎么办 急用钱怎么办啊有没有什么贷款啊 初级会计报名信息表没打印怎么办 电工证复审流程时间过了怎么办 应版权方要求无法下载的电影怎么办 手机设置蜜码小孩都能破解该怎么办 拍到了上海车牌不想买车了怎么办 买车4s店不给临时车牌怎么办 百度云谣绑定的邮箱被绑定了怎么办 百度网盘分享的视频打不开怎么办 在赶集网登录时忘记验证码该怎么办 支付宝绑定的手机号是空号了怎么办 微信聊天后电话号码重复是怎么办 老婆在外省工作不回到我身边怎么办 知道扣扣号怎样盗取他的密码怎么办 喜欢养猫又怕猫破坏家里怎么办 约她她每次都找借口怎么办? 遇到总是找借口不还钱的人怎么办 装修好了业主找借口不给钱怎么办 荒野行动手机换了帐号登不上怎么办 换新手机后微信头像都没有了怎么办 苹果系统维护换不了微信头像怎么办 系统通知栏不显示qq图标怎么办 快递号码写错了而且发货了怎么办 包裹遗忘在郑州东站安检口了怎么办 锁书包的锁头钥匙全掉了怎么办 平板电脑恢复出厂设置变英语怎么办 给国外银行汇款账号写错账号怎么办