时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:1821

解决:528

题目描述:

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
来源:
2007年上海交通大学计算机研究生机试真题

思路:

考察大整数运算中的加减乘法,涉及符号,更繁琐一些。

我的思路用一个含202个int的数组来表示一个数,每个数只存储4位。

(1)只存4位的原因是:如果5位,乘法运算时能到到10位,超过了int的表示范围。

(2)用202个而不是101个的原因是:乘法运算最大可能位数加倍。

最高位表示符号位,其他如果没有数就全置零。

加减法运算选择要看两个数的符号是否相同,并不给予原来算式中的加减法。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define N 202
#define M 10000int prase(char s[], int a[])
{   memset(a, 0, N*sizeof(int));int n = strlen(s);int flag = 0;if (s[0] == '-' || s[0] == '+'){   if (s[0] == '-')flag = 1;s++;    n--;}   int i, j;char t[5];j = 0; for (i=n-1; i>=0; i-=4){int jj = 0;for (int k=i-3; k<=i; k++){if (k>=0) t[jj++] = s[k];}t[jj] = '\0';a[j++] = atoi(t);}a[N-1] = flag;return j;
}       void printA(int a[], int n)
{   int i;for (i=n-1; i>=0; i--){if (a[i] != 0)break;}if (i < 0){printf("0\n");return ;}int flag = a[N-1];if (flag == 1)printf("-");printf("%d", a[i]);for (int j=i-1; j>=0; j--)printf("%04d", a[j]);printf("\n");
}int sub(int a[], int na, int b[], int nb, int c[])
{int i;for (i=0; i<na; i++){if (a[i] < b[i]){a[i+1] --;a[i] += M;}c[i] += a[i]-b[i];}for (i=na-1; i>=0; i--){if (c[i] != 0)break;}return i+1;
}int plus(int a[], int na, int flag, int b[], int nb, int c[])
{memset(c, 0, N*sizeof(int));int nm = (na > nb) ? na : nb;int fa = a[N-1];int fb = (b[N-1]+flag)%2;int i;if (fa == fb){   for (i=0; i<=nm; i++){c[i] += a[i] + b[i];if (c[i] >= M){c[i+1] ++;c[i] -= M;}}c[N-1] = fa;if (c[nm] != 0)return nm + 1;elsereturn nm;}int ns;for (i=nm; i>=0; i--){   if (a[i] != b[i])break;}   if (i < 0)return 0;if (a[i] > b[i]){ns = sub(a, na, b, nb, c);c[N-1] = fa;}else{ns = sub(b, nb, a, na, c);c[N-1] = fb;}return ns;
}   int mult(int a[], int na, int b[], int nb, int c[])
{memset(c, 0, N*sizeof(int));int i, j, k;for (i=0; i<na; i++){for (j=0; j<nb; j++){k = i+j;c[k] += a[i]*b[j];if (c[k] >= M){c[k+1] += c[k]/M;c[k] %= M;}}}c[N-1] = (a[N-1]+b[N-1])%2;for (i=na+nb; i>=0; i--){if (c[i] != 0)break;}return i+1;
}int main(void)
{char sa[2*N], sb[2*N];int na, nb;int a[N], b[N];int np, ns, nm;int p[N], s[N], m[N];while (scanf("%s%s", sa, sb) != EOF){na = prase(sa, a);nb = prase(sb, b);//printA(a, na);//printA(b, nb);np = plus(a, na, 0, b, nb, p); printA(p, np);ns = plus(a, na, 1, b, nb, s);printA(s, ns);nm = mult(a, na, b, nb, m);printA(m, nm);}       return 0;
}
/**************************************************************Problem: 1037User: liangrx06Language: CResult: AcceptedTime:0 msMemory:916 kb
****************************************************************/

转载于:https://www.cnblogs.com/liangrx06/p/5083994.html

九度OJ 1037:Powerful Calculator(强大的计算器) (大整数运算)相关推荐

  1. 打不开磁盘配额linux,九度OJ 1455 珍惜现在,感恩生活 -- 动态规划(背包问题)...

    题目描述: 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买.请问:你用有限的资金最 ...

  2. Freckles - 九度 OJ 1144

    Freckles - 九度 OJ 1144 题目 时间限制:1 秒 内存限制:128 兆 特殊判题:否 题目描述: In an episode of the Dick Van Dyke show, l ...

  3. 非常可乐(九度 OJ 1457)

    非常可乐(九度 OJ 1457) 时间限制:1 秒 内存限制:32 兆 特殊判题:否 1.题目描述: 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是 seeyou 却不这么认为.因为每次当 see ...

  4. 九度OJ 题目1179:阶乘

    /********************************* * 日期:2013-2-8 * 作者:SJF0115 * 题号: 九度OJ 题目1179:阶乘 * 来源:http://ac.jo ...

  5. 九度OJ——1028继续畅通工程

    题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修 ...

  6. Leagal or Not - 九度 OJ 1448

    Leagal or Not - 九度 OJ 1448 题目 时间限制:1 秒 内存限制:128 兆 特殊判题:否 题目描述: ACM-DIY is a large QQ group where man ...

  7. 九度OJ 题目1069:查找学生信息 随笔

    ** 九度OJ 题目1069:查找学生信息 ** 题目描述如下: 输入N个学生的信息,然后进行查询. 输入 输入的第一行为N,即学生的个数(N<=1000) 接下来的N行包括N个学生的信息,信息 ...

  8. 九度OJ 题目1203:IP地址

    /********************************* * 日期:2013-2-8 * 作者:SJF0115 * 题号: 九度OJ 题目1203:IP地址 * 来源:http://ac. ...

  9. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有 ...

最新文章

  1. eclipse lombok插件安装_Eclipse-安装Freemarker插件
  2. 青源 LIVE 第 28 期 | 北大燕博南:下一代AI芯片—存内计算的硬核与软着陆
  3. (传送门)android绘图canvas.clipRect()方法的作用
  4. c#equals方法源码_C#中的Int32.Equals()方法示例
  5. linux内核那些事之虚拟空间划分
  6. 字符串和字符串数组和\0
  7. 4.FreeRTOS学习笔记-消息队列
  8. 环形博弈 HDU - 3951
  9. wildfly jboss deploy 报 拒绝访问
  10. Spring 2.5配置文件详解
  11. 深度学习计算模型中“门函数(Gating Function)”的作用
  12. 线性代数矩阵秩的8大性质、重要定理以及关系
  13. EJB是什么?有什么优点?
  14. 默安科技关键信息基础设施安全防护论文入选中文核心期刊
  15. 终于找到了!AI学习路线图——从零基础到就业
  16. 联想ghost重装系统_联想电脑不能安装GHOST系统
  17. 开幕在即!众星齐聚八泉峡推介会 为山西加油,为二青助力!
  18. [深度学习之CNN]CNN卷积神经网络LeNet-5
  19. com.stardust.autojs.core.web
  20. ubuntu查服务器型号,查看Ubuntu服务器的版本信息

热门文章

  1. Visual C++ 2008入门经典 第二十一章更新数据源
  2. 利用pygments实现django模板内的语法高亮
  3. Python异常处理总结
  4. python创建文件的编码格式
  5. linux下.a/.so/.la目标库区别-转
  6. printf 打印 文件名 函数名 行号
  7. [Java] 1010. Radix (25)-PAT甲级
  8. 蓝桥杯 ADV-182 算法提高 前10名
  9. 【DS】atoi()实现
  10. web.xml 配置 contextConfigLocation