时间限制: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 10000 int 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;
else
return 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: 1037
User: liangrx06
Language: C
Result: Accepted
Time:0 ms
Memory:916 kb
****************************************************************/

九度OJ 1037:Powerful Calculator(强大的计算器) (大整数运算)的更多相关文章

  1. 九度OJ 1340:小A的计算器 (进制转换)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:735 解决:202 题目描述: 以往的操作系统内部的数据表示都是二进制方式,小A新写了一个操作系统,系统内部的数据表示为26进制,其中0-2 ...

  2. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  3. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  4. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  5. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  6. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

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

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

  8. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...

  9. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

随机推荐

  1. Educational Codeforces Round 34 D. Almost Difference【模拟/stl-map/ long double】

    D. Almost Difference time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. Linux 设备驱动模型

    Linux系统将设备和驱动归一到设备驱动模型中了来管理 设备驱动程序功能: 1,对硬件设备初始化和释放 2,对设备进行管理,包括实参设置,以及提供对设备的统一操作接口 3,读取应用程序传递给设备文件的 ...

  3. 【前端阅读】——《活用PHP、MySQL建构Web世界》摘记之高级应用

    一.高级应用 1.计数器 计数器的原理很简单,只有两步: 第一步就是读写一个数字,第二步就是显示出来.一般CGI'大多直接写到文件系统,当然也可以利用MySQL来存储这个数字,完成第一步的操作. 第二 ...

  4. IntelliJ IDEA 识别一个类所属的jar包package

    IntelliJ IDEA 识别一个类所属的jar包package 按住ctrl,鼠标移动上去,不要点击: 有木有快捷键? ctrl+alt+B直接就过去了:需要再跳回来:

  5. API接口管理工具postman等

    国外 postman Swagger:国外比较流行的一款管理工具,英文配置,需要一定的英文基础和服务器搭建基础,学习成本较高. 国内 Apizza: 风格类似postman,熟悉postman的会比较 ...

  6. ios 开发keywordIBInspectable

    这个keyword 能够让开发人员省去非常多事情 把部分界面设置 放在ui设计这里 能够让别人分担 特别是 像我如今所呆的小公司老变样 让他们也能够改 这里仅仅点出一下 有兴趣 自行搜索

  7. 对国外某hotel的内网域简单渗透

    Penetration Testing不单单是一个博客,更热衷于技术分享的平台. 本文将讲述对国外某一hotel的渗透测试,让更多的人安全意识得到提高,有攻才有防,防得在好,也有疏忽的地方,这就是为啥 ...

  8. 想提升java知识的同学请进

    这是我最近在整理的笔记,大家可以看看. https://www.gitbook.com/book/jackal007/java_more/ 如果觉得有问题可以提出,随时修改. 这个笔记是我花了好多时间 ...

  9. Android开发人员不得不收集的代码(转)

    App相关→AppUtils.java 安装App installApp 卸载指定包名的App uninstallApp 获取当前App信息 getAppInfo 获取所有已安装App信息 getAl ...

  10. 性能指标 - OEE

    work center 是指 执行制造作业的资源, 可以是 一个人, 一组人, 一台自动机器, 一组自动机器, 一个半自动机器, 一组半自动机器, 或者是 一个区域组成的生产资源 基本参数 Time ...