1008. 数组元素循环右移问题
来源:互联网 发布:sql数据库程序设计 编辑:程序博客网 时间:2024/04/30 01:15
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1……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
#include<stdio.h>#include<stdlib.h>int main(){ int N,M,i,j=1; int *p; int temp; scanf("%d %d\n",&N,&M); p = (int *)malloc(sizeof(int)*(N+1)); for(i=0;i<N;i++){ scanf("%d",&p[i]); } temp = N-M%N; for(i=temp;i<N;i++){ if(j==1){ printf("%d",p[i]); j=0; } else{ printf(" "); printf("%d",p[i]); } } if(temp==N){ j=1; } for(i=0;i<temp;i++){ if(j==1){ printf("%d",p[i]); j=0; } else{ printf(" "); printf("%d",p[i]); } } return 0;}
附上别人的完美代码:
#include<stdio.h>int main(){ int i,n,right,num[100]; scanf("%d %d",&n,&right); for(i=0;i<n;i++) { right%=n; scanf("%d",&num[right]); right++; } for(i=0;i<n-1;i++) { printf("%d ",num[i]); } printf("%d\n",num[i]);}
https://www.llonely.com/pat-b-1008-array-move-right/
0 0
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008.数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008.数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 页面布局,标签的一些不好的写法
- JDK中rt.jar包下的MANIFEST.MF的源代码
- 再次谈谈TCP的Nagle算法与TCP_CORK选项
- int 与 Integer 主要区别
- java版本 -- java.lang.UnsupportedClassVersionError
- 1008. 数组元素循环右移问题
- C++ string类的一些用法(用到哪更到哪)
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- 【机器学习实战-kNN(k-近邻)】python3实现-书本知识【1】
- fir.im Weekly - 让 iOS 应用更加安全
- java web ssh 整合封装Action Service Dao 层
- HTTP Status 500 - The absolute uri: http://Java.sun.com/jstl/core cannot be resolved...
- Microsoft.AspNetCore.ReportViewer
- Servlet容器(下)