PAT乙级-1011


PAT乙级-1011


题目分析

 本题唯一难点在于输入数值的范围。根据题目,int型的变量刚好无法满足题目要求的数值范围,因此要考虑比int更大的类型。我们可以选择的有long long型和double型。根据C++的规定,long long型至少要比int型表示范围大,因此可以采用;而双精度浮点型double也能够表示比int大的数值范围,因此也可以采用。

 对于多组输入和多组输出,解决的办法是使用一个布尔型数组并全部初始化为false。对于每组输入判断后设置相应位置的布尔数组元素,最后一个for循环按要求输出结果。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "iostream"
#define maxn 15
using namespace std;

int main(){
int n;
cin >> n;
bool results[maxn];
for(int i=0; i<maxn; ++i)
results[i] = false;
double a, b, c;
for(int i=0; i<n; ++i){
cin >> a >> b >> c;
if(a+b > c)
results[i] = true;
}
for(int i=0; i<n; ++i){
cout << "Case #" << i+1 << ": ";
if(results[i])
cout << "true" << endl;
else
cout << "false" << endl;
}
return 0;
}

转载 请注明来源:©Tinshine