1 solutions

  • 0
    @ 2024-6-18 13:52:19

    模拟(90pts)

    • 根据题目要求计算乘方,在计算的过程中判断是否会大于10910^9
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int INF=1e9;
    int main()
    {
        LL a,b;
        cin>>a>>b;
        LL res=1;
        for(int i=1;i<=b;i++) //计算次方
        {
            res=res*a;
            if(res>INF)
            {
                res=-1;
                break;
            }
        }
        cout<<res;
        return 0;
    }
    

    特判+模拟(100pts)

    • 由于1的任意次方都是自己,而2的30次方就会超过10910^9,所以我们可以在循环开始的时候判断如果a>1a \gt 1我们才进行循环。
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int INF=1e9;
    int main()
    {
        LL a,b;
        cin>>a>>b;
        LL res=1;
        for(int i=1;i<=b&&a>1;i++) //计算次方
        {
            res=res*a;
            if(res>INF)
            {
                res=-1;
                break;
            }
        }
        cout<<res;
        return 0;
    }
    
    • 1

    Information

    ID
    1075
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    10
    Tags
    # Submissions
    6
    Accepted
    3
    Uploaded By