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

 

华为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;
}

 

转载于:https://www.cnblogs.com/LUO77/p/5776785.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值