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

 

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

 

posted @ 2016-08-16 16:17  0giant  阅读(1556)  评论(0编辑  收藏  举报