zoukankan      html  css  js  c++  java
  • 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 #define size 100
     3 
     4 //将数组往右移一位
     5 void move_array(int arr[], int length)
     6 {
     7     int i,temp;
     8     temp = arr[length-1];
     9     for (i = 0; i < length -1; ++i)
    10     {
    11         arr[length-1-i] = arr[length-2-i];
    12     }
    13     arr[0] = temp;
    14 }
    15 
    16 int main(int argc, char const *argv[])
    17 {
    18     int a[size], length, times;
    19     while(scanf("%d %d", &length, &times) != EOF)
    20     {
    21         int i;
    22         for (i = 0; i < length; ++i)
    23         {
    24             scanf("%d", &a[i]);
    25         }
    26         for (i = 0; i < (times % length); ++i)
    27         {
    28             move_array(a, length);
    29         }
    30         for (i = 0; i < length; ++i)
    31         {
    32             if(i == 0)
    33                 printf("%d", a[i]);
    34             else
    35                 printf(" %d", a[i]);
    36         }
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    centos7物理机a start job is running for dev-mapper-centosx2dhome.device
    jenkins pipeline流水线
    nginx 加载慢 负载均衡不均衡
    山田预发环境发布脚本
    prometheus 监控容器
    maven私服安装使用
    日志清理
    ERROR 1046 (3D000) at line 1: No database selected
    网络工程学习经典书籍推荐
    每日一句
  • 原文地址:https://www.cnblogs.com/hello-lijj/p/6477716.html
Copyright © 2011-2022 走看看