UVA 113Power of Cryptography

来源:互联网 发布:广州新科佳都科技 知乎 编辑:程序博客网 时间:2024/05/21 01:56

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头文件 climitsint 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;}

参考:

  1. 二分法+高精度——Poj 2109 Power of Cryptography(double型开n次方的方法通过的原因)
  2. 浮点数在计算机中的表示
0 0
原创粉丝点击