0
点赞
收藏
分享

微信扫一扫

PTA刷题笔记(2.5)

正义的杰克船长 2022-02-05 阅读 81
c语言

题目:

一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0​A1​⋯AN−1​)变换为(AN−M​⋯AN−1​A0​A1​⋯AN−M−1​)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?

我的答案(c语言):

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int m,n;
    scanf("%d %d\n",&n,&m);
    int data[n];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&data[i]);
    }
    int temp1,temp2,count,i;
    for(int j=0;j<m;j++)
    {
    temp2=data[0];
    count=0;
    i=0;
    while(count<n)
    {
        if(count%2==0)
        {
            temp1=data[(i+1)%n];
            data[(i+1)%n]=temp2;
        }
        else
        {
            temp2=data[(i+1)%n];
            data[(i+1)%n]=temp1;
        }
        i=(i+1)%n;
        count++;
    }
    }
    for(int i=0;i<n;i++)
    {
        printf("%d",data[i]);
        if(i!=n-1)
        {
            printf(" ");
        }
    }
    return 0;
}
 

举报

相关推荐

PTA刷题笔记(01.30)

python刷题笔记

LeetCode刷题笔记

【leetcode刷题笔记】

刷题笔记篇

Leetcode刷题笔记

刷题笔记2

【Leetcode】刷题笔记

刷题笔记ing

0 条评论