PAT Basic Level 1011. A+B和C(15)

本文详细介绍了C++中各种数据类型的存储空间大小及数值范围,并通过具体示例展示了如何选择合适的数据类型来避免溢出问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【来源】

1011. A+B和C(15)

【分析】

本题看似简单,需要注意数据的范围为[-231, 231],int类型有可能导致溢出。long long int为64位整数,满足题目要求。

【源码】

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i){
        long long a, b, c;
        cin >> a >> b >> c;
        cout << "Case #" << i + 1 << ": ";
        
        if (a + b > c){
            cout << "true\n";
        }
        else{
            cout << "false\n";
        }
    }

    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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值