UVA-113 Power of Cryptography
题目大意:1 <= n <= 200, 1 <= p <= 10^101, k^n = p, 已知 n, p, 求 k。
Sample Input
2
16
3
27
7
4357186184021382204544
Sample Output
4
3
1234
解题思路:这个题思路是先求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;
}
参考: