Given three integers A, B and C in [−263 ,263 ], you are supposed to tell whether A+B>C.
Input Specification:
The first line of the input gives the positive number of test cases, T (≤10). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.
Output Specification:
For each test case, output in one line Case #X: true if A+B>C, or Case #X: false otherwise, where X is the case number (starting from 1).
Sample Input:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false
思路
本以为这是一道大数模拟题目,也用大数模拟的方法做出来了,写完+debug将近两个小时后发现网上大部分题解都是用溢出判断方法做的…但是我这个大数模拟运算方法也ac了,这里贴出来仅供参考。
(做之前稳固一下大整数运算的知识大整数运算)
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
struct bign
{
int d[65];
int len;
bool pos; //pos用来表示正负
bign() {
memset(d, 0, sizeof(d));
len = 0;
pos = true;
}
};
bool judge_num(bign a, bign b);
bool compare(bign a, bign b);
bign set(string);
bign add(bign a, bign b);
bign sub(bign a, bign b);
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
string a, b, c;
cin >> a >> b >> c;
bign n1, n2, n3, t;
n1 = set(a);
n2 = set(b);
t = set(c); //c也写为大数形式方便后面比较
if (n1.pos == true && n2.pos == true)
n3 = add(n1, n2); //两个都是正数则执行相加操作
//一正一负则执行两个正数相减的操作
else if (n1.pos == false && n2.pos == true)
n3 = sub(n2, n1);
else if (n1.pos == true && n2.pos == false)
n3 = sub(n1, n2);
else
{
//两个都是负数,则执行相加操作,但是结果设置为负数
n3 = add(n1, n2);
n3.pos = false;
}
if (compare(n3, t)) //比较计算结果和c的值
cout << "Case #" << i << ": true" << endl;
else
cout << "Case #" << i << ": false" << endl;
}
system("pause");
return 0;
}
//judge_num用来比较绝对值的大小
bool judge_num(bign a, bign b)
{
if (a.len > b.len)
return true;
else if (a.len < b.len)
return false;
else
{
for (int i = a.len - 1; i >= 0; i--)
{
if (a.d[i] > b.d[i])
return true;
else if (a.d[i] < b.d[i])
return false;
}
}
return false;
}
//和judge_num不同,compare的比较是包括符号的
bool compare(bign a, bign b)
{
if (a.pos == true && b.pos == false)
return true;
else if (a.pos == false && b.pos == true)
return false;
else if (a.pos == true && b.pos == true)
return judge_num(a, b);
else
return !judge_num(a, b);
}
bign set(string s) //set用来设置大数形式
{
bign n;
if (s[0] == '-')
{
s = s.substr(1);
n.pos = false;
}
else
n.pos = true;
n.len = s.size();
for (int i = 0; i < s.size(); i++)
n.d[i] = s[n.len - i - 1] - '0';
return n;
}
bign add(bign a, bign b) //执行普通的相加操作
{
bign c;
int carry = 0;
for (int i = 0; i < a.len || i < b.len; i++)
{
c.d[c.len++] = (a.d[i] + b.d[i] + carry) % 10;
carry = (a.d[i] + b.d[i] + carry) / 10;
}
if (carry > 0)
c.d[c.len++] = carry;
c.pos = true;
return c;
}
bign sub(bign a, bign b) //执行相减操作
{
bign c;
if (judge_num(a, b) == false)
//注意这里如果a的绝对值比b小,则交换a和b的内容
//并且提前将结果的符号设置为负数
{
bign d = a;
a = b;
b = d;
c.pos = false;
}
for (int i = 0; i < a.len || i < b.len; i++)
{
if (a.d[i] < b.d[i])
{
a.d[i + 1]--;
a.d[i] += 10;
}
c.d[c.len++] = a.d[i] - b.d[i];
}
while (c.len - 1 >= 1 && c.d[c.len - 1] == 0)
c.len--;
return c;
}
网上找到一份理解溢出判断做法的博客也在这里贴出溢出方法理解