UVA-10025 The ? 1 ? 2 ? … ? n = k problem
题目大意:给定一个数 k (0 ≤ |k| ≤ 1000000000), ? 可以看成 + 或 - ,求 n 的最小值。
Sample Input
2
12
-3646397
Sample Output
7
2701
解题思路:这个题思路是先求1+2+3+……+n>k的最小n,然后判断1+2+……+n的和减去k是否为偶数,若为偶数,则n即为所求,若不是,则n++重复刚才的判断。把k都当做正数做,它们只差了个符号,在本题没有影响。 0 比较特殊,须另外判断。
//UVA-465 Overflow
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstdlib>
using namespace std;
const int inf = INT_MAX; //INT_MAX头文件 climits
int main() {
double a, b;
char s1[1010],s2,s3[1010];
while (scanf("%s %c %s",s1,&s2,s3) != EOF) {
printf("%s %c %s\n",s1,s2,s3);
a = atof(s1); //字符串转换为双精度浮点数(double),头文件 cstdlib
b = atof(s3);
if (s2 == '+') {
if (a > inf)
printf("first number too big\n");
if (b > inf)
printf("second number too big\n");
if (a + b > inf)
printf("result too big\n");
}
if (s2 == '*') {
if (a > inf)
printf("first number too big\n");
if (b > inf)
printf("second number too big\n");
if (a * b > inf)
printf("result too big\n");
}
}
return 0;
}