每天两道算法题
题目
对于3个属于区间 [ -231, 231 ] 的数a,b,c 。请问(a+b)>c是否成立?
举例输入:
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.out
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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef struct abc
{
int32_t a;
int32_t b;
int32_t c;
} abc_t;
int main( void )
{
int n;
/* 输入阶段 */
scanf("%d", &n);
abc_t *arr = malloc( n*sizeof(abc_t) );
for( int i=0; i<n; ++i ) {
scanf("%d %d %d", &arr[i].a, &arr[i].b, &arr[i].c);
}
/* 输出阶段 */
for( int i=0; i<n; ++i ) {
if( (arr[i].a+arr[i].b) > arr[i].c ) {
printf("Case #%d: true\n" , i+1);
}
else {
printf("Case #%d: false\n", i+1);
}
}
free(arr);
return 0;
}
c++
#include <cstdint>
#include <tuple>
#include <list>
#include <iostream>
using namespace std;
list<tuple<int64_t,int64_t,int64_t>> coll;
int main( void )
{
int n;
int64_t a,b,c;
/* 输入阶段 */
cin >> n;
while( n-- > 0 ) {
cin >> a >> b >> c;
tuple<int64_t,int64_t,int64_t> tmp(a,b,c);
coll.push_back(tmp);
}
/* 输出阶段 */
list<tuple<int64_t,int64_t,int64_t>>::iterator pos;
n = 1;
for( pos=coll.begin(); pos!=coll.end(); ++pos ) {
a = get<0>(*pos);
b = get<1>(*pos);
c = get<2>(*pos);
cout << "Case #" << n++ << ": ";
if( (a+b) > c ) {
cout << "true" << endl;
}
else {
cout << "false" << endl;
}
}
}