3987 D. Hacb knows the gift

来源:互联网 发布:js添加css3动画效果 编辑:程序博客网 时间:2024/06/05 12:07

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