我是靠谱客的博主 时尚小懒猪,这篇文章主要介绍1008 数组元素循环右移问题 (20 分)--数组操作,现在分享给大家,希望可以做个参考。

1008 数组元素循环右移问题 (20 分)–数组操作

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

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

输入样例:
6 2
1 2 3 4 5 6
输出样例:
5 6 1 2 3 4

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class _1008 { //分析:不需要遵循题目意思将数组循环右移,我们要得到的只是输出结果正确。 // 直接在第一遍读入数字的时候按照输出结果的顺序存入数组中即可。 // // 有几个测试点没通过,究其原因 // 。。。。。int[] arr=new int[6]-->int[] arr=new int[N]竟然是这种低级错误 static StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } public static void main(String[] args) throws IOException { int N=nextInt(); int M=nextInt(); int[] arr=new int[N]; for(int i=0;i<N;i++) { int k=(i+M)%N; //计算出右移后的数组中的位置 arr[k]=nextInt(); } for(int i=0;i<arr.length;i++) { if(i==arr.length-1) { System.out.print(arr[i]); }else { System.out.print(arr[i]+" "); } } } }

最后

以上就是时尚小懒猪最近收集整理的关于1008 数组元素循环右移问题 (20 分)--数组操作的全部内容,更多相关1008内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(107)

评论列表共有 0 条评论

立即
投稿
返回
顶部