PAT(Advanced) 1065 A+B and C (64bit) (20)

Given three integers A, B and C in [-2^63^, 2^63^], 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&gtC, or "Case #X: false" otherwise, where X is the case number (starting from 1).

解答:

方法一:【string模拟】啊,题目真的是简洁明了,我先用long long 试了下,两个测试用例没过,然后我就用string来模拟大整数运算了(相当蠢)。。然后自己把过程实现了一遍,在比较两个数字字符串(都是正数)的大小时,先要比长短,长的字符串当然大;如果一样长,再比较字典序。终于AC。

方法二:【理解64bit整数加减运算的计算机表示】64bit可以用long long来表示,但是会出现越界的情况,但越界仅仅两种,一是两个正整数相加可能得到负值,另外一个是两个负整数相加可能得到正值,分别对这两种情况单独判定即可。

方法一AC代码如下:

#include<cstdio>
#include<iostream>
#include<string>
#include<cctype>
#include<algorithm>

using namespace std;
string add(string a, string b)   //返回两个正字符串的和(字符串形式)
{
	string c;
	reverse(a.begin(), a.end());
	reverse(b.begin(), b.end());
	int len1 = a.length(), len2 = b.length();
	if(len1 > len2) b += string(len1 - len2, '0');
	if(len1 < len2) a += string(len2 - len1, '0');
	int incre = 0;
	for(int i = 0; i < a.size(); ++i)
	{
		char ch = a[i] + b[i] - '0' + incre;
		if(ch > '9'){
			incre = 1;
			ch = ch - 10;
		}
		else
		{
			incre = 0;
		}
		c.push_back(ch);
	}
	if(incre == 1)
	{
		c.push_back('1');
	}
	reverse(c.begin(), c.end());
	return c;
}
bool cmp(string a, string b)      
{
    //比较两个字符串大小
	if(a.length() > b.length()) return true;
	else if(a.length() < b.length()) return false;
	else return a > b;
}
bool judge(string& a, string& b, string& c)
{
	bool ahead, bhead, chead;
	
	ahead = isdigit(a[0]); 
	bhead = isdigit(b[0]);
	chead = isdigit(c[0]);
	
	if(ahead && bhead && chead) return ( cmp(add(a,b), c) );
	if(ahead && bhead && !chead) return true;
	if(ahead && !bhead && chead) return ( cmp(a, add(b.substr(1),c)) );
	if(ahead && !bhead && !chead) return ( cmp(add(a,c.substr(1)), b.substr(1)) );
	
	if(!ahead && bhead && chead) return ( cmp(b, add(a.substr(1),c)) );
	if(!ahead && bhead && !chead) return ( cmp(add(b,c.substr(1)), a.substr(1)) );
	if(!ahead && !bhead && chead) return false;
	if(!ahead && !bhead && !chead) return ( cmp(c.substr(1), add(a.substr(1),b.substr(1))) );
}

int main()
{
	int N;
	
	scanf("%d", &N);
	for(int i = 1; i <= N; ++i)
	{
		string a, b, c;
		
		cin >> a >> b >> c;
		printf("Case #%d: ", i);  //Case #1: false
		judge(a, b, c) ? printf("true\n") : printf("false\n");
	}
	return 0;
}

方法二AC代码如下:

#include<cstdio>
#include<iostream>

#define ll long long

using namespace std;

bool judge(ll a, ll b, ll c)
{
	long long re = a+b; //a+b的结果需要用re保存下来 
	if(a>0 && b>0 && re<=0) return true;
	else if(a<0 && b<0 && re>=0) return false;
	else return (re > c);
}
int main()
{
	int N;
	
	scanf("%d", &N);
	for(int i = 1; i <= N; ++i)
	{
		long long a, b, c;
		
		cin >> a >> b >> c;
		printf("Case #%d: ", i);  //输出格式:Case #1: false
		judge(a, b, c) ? printf("true\n") : printf("false\n");
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值