P8813 70分求助

题目总版

xjhjtz0320 @ 2024-10-06 11:19:28

#include <bits/stdc++.h> 
using namespace std; 
int main()
{
    long long c=0;
    int a,b;
    cin>>a>>b;
    c=a;
    if(b==1){
        cout<<a;
        return 0;
    }
    if(b==0)
    {
        cout<<0;
        return 0;
    }
    for(int i=2;i<=b;i++)
        c*=a;
    if(c>1e9) cout<<"-1";
        else cout<<c;
}

by Scez @ 2024-10-06 11:22:01

@xjhjtz0320

#include <bits/stdc++.h> 
using namespace std; 
int main()
{
    long long c=1;
    int a,b;
    cin>>a>>b;
    c=a;
    if(b==1){
        cout<<a;
        return 0;
    }
    if(b==0)
    {
        cout<<1;
        return 0;
    }
    for(int i=2;i<=b;i++)
    {
        c*=a;
        if(c>1e9)
        {
            cout<<-1;
            return 0;
        }
    }
    cout<<c;
}

by Scez @ 2024-10-06 11:23:20

@xjhjtz0320

c应该赋值为1

边乘边判断小心爆longlong


by Scez @ 2024-10-06 11:24:34

@Scez 应该是防止爆longlong(语文不好见谅


|