题目要求3 个整数 A、B 和 C给定区间 [−2^31,2^31] 内
而int的范围包含于这个区间,并不包含,所以需要用更大精度的变量类型,那么用float吗?
当用float时是以下情况:
2147483647 0 2147483646
Case #1: false
而真正的答案应是:
2147483647 0 2147483646
Case #1: true
这又扯到精度的问题了,可以看下这篇转载的文章:c/c++中int等的数据范围_柯微的博客-CSDN博客_c++ int范围
此处我用的double,还可以使用long long int,不妨在 VS 中DEBUG试一试区别
#include<iostream>
using namespace std;
int main()
{
int numofCase;
double a,b,c;
cin>>numofCase;
for(int i = 1;i <= numofCase;i++)
{
cin>>a>>b>>c;
if(a + b > c)
printf("Case #%d: true\n",i);
else
printf("Case #%d: false\n",i);
}
return 0;
}