• 1008. 数组元素循环右移问题 (20)


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

    输入格式:每个输入包含一个测试用例,第1行输入N ( 1<=N<=100)、M(M>=0);第2行输入N个整数,之间用空格分隔。

    输出格式:在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。

    输入样例:

    6 2
    1 2 3 4 5 6
    

    输出样例:

    5 6 1 2 3 4
    
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <iostream>
     4 #include <string.h>
     5 #include <string>
     6 #include <math.h>
     7  
     8 
     9 int main(){
    10     int a[110];
    11     int n,m,count=0;
    12     scanf("%d%d",&n,&m);
    13     m=m%n; 
    14     for(int i=0;i<n;i++)
    15     {
    16         scanf("%d",&a[i]);
    17     }
    18     for(int i=n-m;i<n;i++)
    19     {
    20         printf("%d",a[i]);
    21         count++;
    22         if(count<n)printf(" ");
    23     }
    24     for(int i=0;i<n-m;i++)
    25     {
    26         printf("%d",a[i]);
    27         count++;
    28         if(count<n)printf(" ");
    29     }
    30     return 0;
    31 }
  • 相关阅读:
    [转载]小谈网络游戏同步
    [ASE][Daily Scrum]11.06
    [Proposal]Tank Battle——Infinite
    [proposal][app]Watch your time!
    [Proposal]Nano-Diary(纳日记)
    LaTeX中用BibTex管理参考文献
    matlab化简符号表达式
    placeholder颜色变化
    链接图片外边出现蓝框(IE8/IE9/IE10)
    图标排列
  • 原文地址:https://www.cnblogs.com/ligen/p/4297887.html
Copyright © 2020-2023  润新知