编程题目: PAT 1011. A+B和C (15)

1011. A+B和C (15)

时间限制
50 ms
内存限制
32000 kB
代码长度限制
8000 B
判题程序
Standard
作者
HOU, Qiming

给定区间[-231, 231]内的3个整数A、B和C,请判断A+B是否大于C。

输入格式:

输入第1行给出正整数T(<=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。

输出格式:

对每组测试用例,在一行中输出“Case #X: true”如果A+B>C,否则输出“Case #X: false”,其中X是测试用例的编号(从1开始)。

输入样例:
4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647
输出样例:
Case #1: false
Case #2: true
Case #3: true
Case #4: false
         本题乍一看,简单的让人难过!稍微仔细看一下,考察的点应该就在于数的范围给的比较大,这样随之而来的可能会有A+B的溢出问题,所以单纯的用int型应该是不合适的,考虑用long int,或long long int ,经过试验long int是可行的。

/*
http://pat.zju.edu.cn/contests/pat-b-practise/1011
*/
#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int num;
	long int a,b,c;
	cin>>num;
	vector<int> flag(10);
	int i;
	for(i =0;i<num;i++)
	{
		cin>>a>>b>>c;
		flag[i]= a+b>c ? 1:0;
	}
	for(i=0;i<num;i++)
	{
		cout<<"Case #"<<i+1<<": ";
		if(flag[i])
			cout<<"true"<<endl;
		else
			cout<<"false"<<endl;
	}

	system("pause");
	return 0;
}

        另外,在看了别人的阶梯办法之后,这里因引入一下,针对AB的符号分别进行分析,贴一下转载的代码。

//PAT1011
//这题就是死磕自己....
//考虑a,b同号溢出问题,若同号转a > c - b, 异号直接相加判断

#include <iostream>

using namespace std;

inline bool isSame(int a, int b)
{
	return ( a > 0 && b > 0 ) || ( a < 0 && b < 0 );
}

int main()
{
	int a, b, c;
	int t;
	int cnt = 1;
	cin>>t;
	while ( cnt <= t )
	{
		cin>>a>>b>>c;
		bool flag = false;
		if ( isSame(a, b) )			//若a,b同号
		{
			if ( a > 0 )
			{
				if ( c > 0 )		//a, b, c > 0
					flag = ( a > (c - b) );		//a + b > c 转为 a>(c -b )
				else				//a, b > 0, c <= 0, 结果必为真
					flag = true;
			}
			else
			{
				if ( c < 0 )		//同上,a,b,c < 0
					flag = ( a > (c - b) );
				else				//同上a, b < 0, c >= 0, 结果必为真
					flag = false;
			}
		}
		else						//a,b异号
			flag =  ( a + b > c );
		if ( flag )
		{
			cout<<"Case #"<<cnt++<<": true"<<endl;
		}
		else
		{
			cout<<"Case #"<<cnt++<<": false"<<endl;
		}
	}
	return 0;
}

        另外,作为延伸,贴一下C++中各个数据类型所占的空间大小以及所能表示的数的范围。获得以下结果的代码一同给出(非原创)。


#include<iostream>
#include<string>
#include <limits>
using namespace std;

int main()
{

    cout << "bool: \t\t" << "Bytes:" << sizeof(bool);
    cout << "\tMAX:" << (numeric_limits<bool>::max)();
    cout << "\t\tMIN:" << (numeric_limits<bool>::min)() << endl;
    cout << "char: \t\t" << "Bytes:" << sizeof(char);
    cout << "\tMAX:" << (numeric_limits<char>::max)();
    cout << "\t\tMIN:" << (numeric_limits<char>::min)() << endl;
    cout << "signed char: \t" << "Bytes:" << sizeof(signed char);
    cout << "\tMAX:" << (numeric_limits<signed char>::max)();
    cout << "\t\tMIN:" << (numeric_limits<signed char>::min)() << endl;
    cout << "unsigned char: \t" << "Bytes:" << sizeof(unsigned char);
    cout << "\tMAX:" << (numeric_limits<unsigned char>::max)();
    cout << "\t\tMIN:" << (numeric_limits<unsigned char>::min)() << endl;
    cout << "wchar_t: \t" << "Bytes:" << sizeof(wchar_t);
    cout << "\tMAX:" << (numeric_limits<wchar_t>::max)();
    cout << "\t\tMIN:" << (numeric_limits<wchar_t>::min)() << endl;
    cout << "short: \t\t" << "Bytes:" << sizeof(short);
    cout << "\tMAX:" << (numeric_limits<short>::max)();
    cout << "\t\tMIN:" << (numeric_limits<short>::min)() << endl;
    cout << "int: \t\t" << "Bytes:" << sizeof(int);
    cout << "\tMAX:" << (numeric_limits<int>::max)();
    cout << "\tMIN:" << (numeric_limits<int>::min)() << endl;
    cout << "unsigned: \t" << "Bytes:" << sizeof(unsigned);
    cout << "\tMAX:" << (numeric_limits<unsigned>::max)();
    cout << "\tMIN:" << (numeric_limits<unsigned>::min)() << endl;
    cout << "long: \t\t" << "Bytes:" << sizeof(long);
    cout << "\tMAX:" << (numeric_limits<long>::max)();
    cout << "\tMIN:" << (numeric_limits<long>::min)() << endl;
    cout << "unsigned long: \t" << "Bytes:" << sizeof(unsigned long);
    cout << "\tMAX:" << (numeric_limits<unsigned long>::max)();
    cout << "\tMIN:" << (numeric_limits<unsigned long>::min)() << endl;
    cout << "long long: \t" << "Bytes:" << sizeof(long long);
    cout << "\tMAX:" << (numeric_limits<long long>::max)();
    cout << "\tMIN:" << (numeric_limits<long long>::min)() << endl;
    cout << "double: \t" << "Bytes:" << sizeof(double);
    cout << "\tMAX:" << (numeric_limits<double>::max)();
    cout << "\tMIN:" << (numeric_limits<double>::min)() << endl;
    cout << "long double: \t" << "Bytes:" << sizeof(long double);
    cout << "\tMAX:" << (numeric_limits<long double>::max)();
    cout << "\tMIN:" << (numeric_limits<long double>::min)() << endl;
    cout << "float: \t\t" << "Bytes:" << sizeof(float);
    cout << "\tMAX:" << (numeric_limits<float>::max)();
    cout << "\tMIN:" << (numeric_limits<float>::min)() << endl;
    cout << "size_t: \t" << "Bytes:" << sizeof(size_t);
    cout << "\tMAX:" << (numeric_limits<size_t>::max)();
    cout << "\tMIN:" << (numeric_limits<size_t>::min)() << endl;
    cout << "string: \t" << "Bytes:" << sizeof(string) << endl;

    system("pause");
    return 0;
}


posted @ 2014-07-02 13:51  F8Master  阅读(219)  评论(0编辑  收藏  举报