• 翻转数组/循环移动类题


    题目参见 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];
  • 相关阅读:
    UIView添加手势
    UIView常见属性设置汇总
    关于页面传值
    有关segue的简介
    alloc
    如何定义静态方法
    一座小城
    清明
    开通博客
    iOS学习之界面间传值
  • 原文地址:https://www.cnblogs.com/lxzbky/p/12467893.html
Copyright © 2020-2023  润新知