3987 D. Hacb knows the gift

Coach Yu will send gifts to ACM teammates. This message was known by Hacb, he send the message to G.D.R at once. But the message was encrypted. If Hacb wants to send an integer x to G.D.R, the massage will be encrypted as F(x) which F(x) = x^3. Now G.D.R get the F(x), please help him to get the message x.
Hoverer, the message may be changed by error. So if you can’t find the x which satisfy F(x) = x^3 you should output “Wrong Message!”
Input

First line is a number t . Then t lines follows. Each line contains a number n, |n|≤10^18
Output

Output the number x on a line which x^3 = n ,but if the x not exist output “Wrong Message!”
Output a line between two results.
Sample Input

2
8
7
Sample Output

2

Wrong Message!

题意:求一个数是否能开3次方。

用二分法来做。(要注意输出格式,负数的时候,0的时候)左边有0开始,右边由1e6开始。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main (void)
{
    int n;
    cin>>n;
    while(n--)
    {
        int flag=1;int mark=0;
        long long num;
        scanf("%lld",&num);
        if(num<0)
        {
            flag=-1;
            num=num*(-1);
        }
        long long lb=0,ub=1e6;
        while(ub-lb>1)
        {
            int mid=(lb+ub)/2;
            /*if(num%(mid*mid)!=0)
                {
                    mark=1;
                    break;  
                }*/
            if(num/(mid*mid)<=mid)//乘法改除法
                {
                    ub=mid;
                }
            else
            {
                lb=mid;
            }
            //lb+1=ub;
        }
        if(ub*ub*ub==num||lb*lb*lb==num)
        {
            if(ub*ub*ub==num)
                printf("%lld\n",ub*flag);
            if(lb*lb*lb==num)
                printf("%lld\n",lb*flag);
        }
        else
            printf("Wrong Message!\n");
        if(n!=0)
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值