UVA 113 Power of Cryptography

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;
}

参考:

  1. 二分法+高精度——Poj 2109 Power of Cryptography(double型开n次方的方法通过的原因)
  2. 浮点数在计算机中的表示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值