警示后人加特判
查看原帖
警示后人加特判
1286953
hoyizh楼主2024/7/7 11:01

错误代码

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

中间只隔一个特判

2024/7/7 11:01
加载中...