码迷,mamicode.com
首页 > 移动开发 > 详细

翻转数组/循环移动类题

时间:2020-03-12 11:32:25      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:pat   pac   begin   temp   white   turn   函数   height   循环   

题目参见 PAT (Basic Level) Practice (中文) 1008 数组循环右移M个元素
 
核心函数
void reverse(int a[], int n, int begin, int end)
{
    int temp = 0;
    if (begin >= end)
        return;
    for (int i = begin; i <=(begin + end)/2; i++)
    {
        temp = a[i];
        a[i] = a[end - i+begin];
        a[end - i + begin] = temp;
    }
}

    int n, m,a[max];
    cin >> n >> m;
    m = m%n;//缩小到最小次数
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    reverse(a, n, 0, n - 1 - m);
    reverse(a, n, n-m, n-1);
    reverse(a, n, 0, n-1);
 
 
    for (int i = 0; i < n-1; i++)
    {
        cout << a[i] << ‘ ‘;
    }
    cout << a[n - 1];

翻转数组/循环移动类题

标签:pat   pac   begin   temp   white   turn   函数   height   循环   

原文地址:https://www.cnblogs.com/lxzbky/p/12467893.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!