zoukankan      html  css  js  c++  java
  • 编程题目: 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: 		" << "Bytes:" << sizeof(bool);
        cout << "	MAX:" << (numeric_limits<bool>::max)();
        cout << "		MIN:" << (numeric_limits<bool>::min)() << endl;
        cout << "char: 		" << "Bytes:" << sizeof(char);
        cout << "	MAX:" << (numeric_limits<char>::max)();
        cout << "		MIN:" << (numeric_limits<char>::min)() << endl;
        cout << "signed char: 	" << "Bytes:" << sizeof(signed char);
        cout << "	MAX:" << (numeric_limits<signed char>::max)();
        cout << "		MIN:" << (numeric_limits<signed char>::min)() << endl;
        cout << "unsigned char: 	" << "Bytes:" << sizeof(unsigned char);
        cout << "	MAX:" << (numeric_limits<unsigned char>::max)();
        cout << "		MIN:" << (numeric_limits<unsigned char>::min)() << endl;
        cout << "wchar_t: 	" << "Bytes:" << sizeof(wchar_t);
        cout << "	MAX:" << (numeric_limits<wchar_t>::max)();
        cout << "		MIN:" << (numeric_limits<wchar_t>::min)() << endl;
        cout << "short: 		" << "Bytes:" << sizeof(short);
        cout << "	MAX:" << (numeric_limits<short>::max)();
        cout << "		MIN:" << (numeric_limits<short>::min)() << endl;
        cout << "int: 		" << "Bytes:" << sizeof(int);
        cout << "	MAX:" << (numeric_limits<int>::max)();
        cout << "	MIN:" << (numeric_limits<int>::min)() << endl;
        cout << "unsigned: 	" << "Bytes:" << sizeof(unsigned);
        cout << "	MAX:" << (numeric_limits<unsigned>::max)();
        cout << "	MIN:" << (numeric_limits<unsigned>::min)() << endl;
        cout << "long: 		" << "Bytes:" << sizeof(long);
        cout << "	MAX:" << (numeric_limits<long>::max)();
        cout << "	MIN:" << (numeric_limits<long>::min)() << endl;
        cout << "unsigned long: 	" << "Bytes:" << sizeof(unsigned long);
        cout << "	MAX:" << (numeric_limits<unsigned long>::max)();
        cout << "	MIN:" << (numeric_limits<unsigned long>::min)() << endl;
        cout << "long long: 	" << "Bytes:" << sizeof(long long);
        cout << "	MAX:" << (numeric_limits<long long>::max)();
        cout << "	MIN:" << (numeric_limits<long long>::min)() << endl;
        cout << "double: 	" << "Bytes:" << sizeof(double);
        cout << "	MAX:" << (numeric_limits<double>::max)();
        cout << "	MIN:" << (numeric_limits<double>::min)() << endl;
        cout << "long double: 	" << "Bytes:" << sizeof(long double);
        cout << "	MAX:" << (numeric_limits<long double>::max)();
        cout << "	MIN:" << (numeric_limits<long double>::min)() << endl;
        cout << "float: 		" << "Bytes:" << sizeof(float);
        cout << "	MAX:" << (numeric_limits<float>::max)();
        cout << "	MIN:" << (numeric_limits<float>::min)() << endl;
        cout << "size_t: 	" << "Bytes:" << sizeof(size_t);
        cout << "	MAX:" << (numeric_limits<size_t>::max)();
        cout << "	MIN:" << (numeric_limits<size_t>::min)() << endl;
        cout << "string: 	" << "Bytes:" << sizeof(string) << endl;
    
        system("pause");
        return 0;
    }


  • 相关阅读:
    第三天 python的初始编码,基本数据类型(int,str,bool),字符串的操作
    while循环,格式化输出,运算符,while...else...
    python的种类,变量,常量,基础数据类型,input,if条件语句
    JavaScript 之 web API
    传输层上的TCP和UDP
    应用层上的协议HTTP
    计算机网络通信
    grid布局
    使用classList和dataset实现tab切换
    JS之跨域
  • 原文地址:https://www.cnblogs.com/f8master/p/3826091.html
Copyright © 2011-2022 走看看