PAT (Basic Level) Practice (中文)1011 A+B 和 C (15 分)
题目
给定区间 [−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
C++实现
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int t;
cin>>t;
bool result[10];
memset(result,0, sizeof(result));
for (int i = 0; i < t; ++i) {
long long a,b,c;
cin>>a>>b>>c;
if (a+b>c) result[i]= true;
else result[i]= false;
}
for (int n = 0; n < t; ++n) {
if (result[n]) cout<<"Case #"<<n+1<<": true"<<endl;
else cout<<"Case #"<<n+1<<": false"<<endl;
}
return 0;
}
python实现
a=int(input())
m=[]
n=[]
for i in range(a):
m=input().split()
n.append(m)
for i in range(a):
x=n[i]
sum1=int(x[0])+int(x[1])
sum2=int(x[2])
if sum1>sum2:
print("Case #{}: {}".format(i+1,"true"))
else:
print("Case #{}: {}".format(i+1,"false"))