错误代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
long long n,m,s=1;
cin>>n>>m;
if(n>=sqrt(10e9)&&m>=2)
{
cout<<-1;
return 0;
}
for(int i=1;i<=m;i++)
{
s*=n;
if(s>10e9||s<n)
{
cout<<-1;
return 0;
}
}
cout<<s;
return 0;
}
正确代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
long long n,m,s=1;
cin>>n>>m;
if(n == 1)
{
cout << 1 << endl;
return 0;
}
if(m == 1)
{
cout << n << endl;
return 0;
}
if(n > 31622)
{
cout << -1 << endl;
return 0;
}
if(m > 29)
{
cout << -1 << endl;
return 0;
}
for(int i=1;i<=m;i++)
{
s*=n;
if(s>10e9||s<n)
{
cout<<-1;
return 0;
}
}
cout<<s;
return 0;
}
中间只隔一个特判