TJU 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!

题意概述:给定一个n, |n|≤10^18,求其立方根。若其立方根不是整数则输出“Wrong Message!”,否则输出其立方根。

解题思路:在c语言的cmath库中,有一个pow函数。原型为:double pow(double x, double y); 计算以x为底数的y次幂。
 
 
我的代码:

#include<iostream>
#include<cmath>
#define eps 1e-6    //精确度 
using namespace std;
int main()
{
    long long int n;  //可以到10的30次方 
    int t;
    cin>>t;
    while(t--)
    {
        int flag=1;  //标记正负 
        cin>>n;
        if(n<0)
        {
                flag=-1;
                n=-n;
        }
        double x=pow(1.0*n,1.0/3.0);
        int y=int (pow(1.0*n,1.0/3.0)+eps);  
        //这一步至关重要,否则会出现莫名其妙的错误。如果不加eps,可能会出现double变量的值为9,付给int变量后却是8。
        //这种错误可能和两种变量的存储方式不同,具体不可知 
        if(x-y<eps&&x-y>-eps)cout<<flag*y<<endl;  //精确度 
        else cout<<"Wrong Message!"<<endl;
        if(t!=0)cout<<endl;
    }
    system("pause");
    return 0;
} 


大神的代码1:


#include<cstdio>
#include<cstring>
#include<cmath>
#define eps 1e-6    //相当于0.000001 
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long int n;
        int f=1;
        scanf("%lld",&n);
        if(n<0)
        {
            f=0;
            n=-n;
        }
        double w=pow(1.0*n,1.0/3.0);
        int  m=int (pow(1.0*n,1.0/3.0)+eps);
        if(w-m<eps&&w-m>-eps)
        {
            if(f==0)printf("%d\n",-m);
            else printf("%d\n",m);
        }
        else
        puts("Wrong Message!");
        if(t!=0)
        puts("");
    }
}


大神的代码2:


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    long long int TT,t,u,l;
    cin>>TT;
    int flag = 0;
    while(TT--)
    {
        if(flag == 1) cout<<endl;
        flag = 1;
        cin>>t;
        if(t < 0) t = -t,l=-1;
        else l = 1;
        double y = t;
        y = pow(y,1.0/3);
        long long int ans = y;
        for(u = ans - 10;u < ans+10;u++)
            if(u*u*u == t){ cout<<l*u<<endl;break;}
        if(u == ans + 10) cout<<"Wrong Message!"<<endl;


    }
    return 0;
}

我觉得这里值得一提的是最后一段代码,思路非常好。先确定一个大致范围,用循环的方式找到满足题意的数;若找不到输出“Wrong Message!”

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值