• 【每天一道算法题】循环移动


    华为OJ上的题,始终有一个test没过,不知道为什么- - |||

    也考虑移动次数为负值,大于10,0,10这些边界情况了。只是一直没有移动数组,因为只是需要输出嘛。

    #include <iostream>
    using namespace std;
    
    int main(){
        int arr[10];
        int sum=0;
        for(int i=0;i<10;i++){
            cin>>arr[i];
            sum+=arr[i];
        }
        int step=0;
        cin>>step;
        
        //向前移动,先输出后m个和,再输出前m个和
        int sum1=0,sum2=0;
        if(step<0){
            step=-1*step;
        for(int i=step-1,j=0;i<10;i++){
            if(j++<step)
                sum1+=arr[i];
            cout<<arr[i]<<" ";}
        for(int j=0;j<step;j++){
            sum2+=arr[j];
            if(j!=step-1)
                cout<<arr[j]<<" ";
            else cout<<arr[j]<<endl;
        }
        cout<<sum2<<" "<<sum1<<endl;}
    
        //向后移动,先输出前m个,再输出后m个
        else if(step>=0){
            int move=step;
            if(step>=10)
                step%=10;
            for(int i=step;i>0;i--){
                sum1+=arr[10-i];
                cout<<arr[10-i]<<" ";
            }
            for(int j=0;j<10-step;j++){
                if(j>=10-step-step)
                    sum2+=arr[j];
                if(j!=10-step-1)
                    cout<<arr[j]<<" ";
                else cout<<arr[j]<<endl;
            }
            if(move<10)
                cout<<sum1<<" "<<sum2<<endl;
            else cout<<sum<<" "<<sum<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Keepalived 无法自动转换主备角色,请关注 iptables 防火墙配置
    Linux 下使用网易的SMTP服务器 发送邮件
    Spring-boot 最小demo
    go build 时报错 cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
    spark-shell 执行脚本并传入参数
    JVM
    spark
    spark
    linux
    linux
  • 原文地址:https://www.cnblogs.com/LUO77/p/5776785.html
Copyright © 2020-2023  润新知