15 条题解

  • 1
    @ 2025-4-14 21:19:30

    程序绝对严谨,步步有原因。 求赞~~

    #include<bits/stdc++.h>
    using namespace std;
    long long k[1],a,b,ans1=0,ans2=0,ans,c1,c2;
    /*long long类型防止数据大小溢出int范围
     证明:(10^18)*4=4000000000000000000,int范围(2^31)-1=2147483647
     2147483647<4000000000000000000 所以用long long
    */
    int main(){ //主程序
        cin>>a>>b; //输入a和b
        for(int i=1;i<=a;i++)
            ans1++;  //“掰手指”算法求a的值
        for(int i=1;i<=b;i++)
            ans2++;  //同上
        if(ans1==a && ans2==b){
            c1=0; 
            c2=c1;  //处理数组k在这里所对应的下标(重要!!!)
            k[c1]+=ans1; 
            k[c2]+=ans2; //k[0]在main函数外定义,已自动清零,用k[下标]+=……避免ans1的值被覆盖,以此求和
        }
        if((ans1<0) ||
           (ans2<0) ||
           (ans2+ans1<0) //多层表达式严格扫除错误情况
        ){
            cout<<"Error"<<endl; //避免题目测试数据有误,确保答案正确
            return 0;
        }
        else if((a<=-1) ||
                (b<=-1) || 
                (a+b<0)  //检查初始输入值,防止运算过程中出现错误
            ){
                cout<<"Compute Error"<<endl; //compute意思:计算
                return 0;
            }
        int cmax=c1;
        if(c2>cmax)
            cmax=c2; //打擂法求出最大的下标,同时保证输出答案正确
        if(cmax<0){
            cout<<"Eroor";
            return 0; //return 0;提前退出程序,防止重复输出或后面输出错误答案(不要漏!!!)
        }
        for(int i=0;i<=cmax;i++)
            cout<<k[i]; //根据最大下标输出计算结果
        for(int i=0;i<=cmax;i++)
            cout<<" "; //打个格式,确保使用的下标为0(为0就不会打空格),如果错了dev编译界面能看出来
        cout<<endl; //作者习惯
        return 0;
    }
    //本人亲测已过,语言为C++17(O2)
    

    信息

    ID
    4451
    时间
    1000ms
    内存
    128MiB
    难度
    1
    标签
    递交数
    1691
    已通过
    578
    上传者