当前位置:网站首页>Daily practice------There are n integers, so that the previous numbers are moved back m positions in order, and the last m numbers become the first m numbers
Daily practice------There are n integers, so that the previous numbers are moved back m positions in order, and the last m numbers become the first m numbers
2022-08-02 03:15:00 【Bei Ning Mo language】
题目: 有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
解题关键:It is necessary to create a new array so that the original array can be moved backward as a wholeM位
思路:1.创建个有n个整数的数组
2.输出n个数字,存储到数组中
3.遍历原数组
4.The sequence of the previous numbers in the output is shifted backwardm个位置
5.Create a new array of length (n+m)
6.Store the values of the old array into the new array
7.move the whole array backwardsm位
8.m个数变成最前面的m个数
9.遍历新数组
过程: 接下来我们根据我们的解题思路来一步步写代码
1.创建个有n个整数的数组
Scanner sc = new Scanner(System.in);
System.out.println("Please enter how many integers you need to enter:");
int n = sc.nextInt();
int[] nums = new int[n];
2.输出n个数字,存储到数组中
System.out.println("请依次输入" + n + "个整数");
for (int i = 0; i < nums.length; i++) {
System.out.println("第" + (i + 1) + "个数字为:");
int num = sc.nextInt();
nums[i] = num;
}
3.遍历原数组
System.out.print("原数组为:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
System.out.println();// 换行
4.The sequence of the previous numbers in the output is shifted backwardm个位置
System.out.print("Output the position where the preceding numbers are sequentially moved backwards:");
int m = sc.nextInt();
5.Create a new array of length (n+m)
int[] newNums = new int[n + m];
6.Store the values of the old array into the new array
for (int i = 0; i < nums.length; i++) {
newNums[i] = nums[i];
}
System.out.println();
7.move the whole array backwardsm位
for (int i = newNums.length - 1; i-m >= 0 ; i--) {
newNums[i] = newNums[i-m];
}
8.m个数变成最前面的m个数
for (int i = 0; i < m; i++) {
newNums[i] = newNums[newNums.length -(m-i)];
}
9.遍历新数组
System.out.print("新数组为:");
for (int i = 0; i < nums.length; i++) {
nums[i] = newNums[i];
System.out.print(nums[i] + " ");
}
完整结果如下:

为了方便大家使用,下面附上源码:
// 1.创建个有n个整数的数组
Scanner sc = new Scanner(System.in);
System.out.println("Please enter how many integers you need to enter:");
int n = sc.nextInt();
int[] nums = new int[n];
// 2.输出n个数字,存储到数组中
System.out.println("请依次输入" + n + "个整数");
for (int i = 0; i < nums.length; i++) {
System.out.println("第" + (i + 1) + "个数字为:");
int num = sc.nextInt();
nums[i] = num;
}
// 3.遍历原数组
System.out.print("原数组为:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
System.out.println();// 换行
// 4.The sequence of the previous numbers in the output is shifted backwardm个位置
System.out.print("Output the position where the preceding numbers are sequentially moved backwards:");
int m = sc.nextInt();
// 5.Create a new array of length (n+m)
int[] newNums = new int[n + m];
//6.Store the values of the old array into the new array
for (int i = 0; i < nums.length; i++) {
newNums[i] = nums[i];
}
System.out.println();
//7.move the whole array backwardsm位
for (int i = newNums.length - 1; i-m >= 0 ; i--) {
newNums[i] = newNums[i-m];
}
//8.m个数变成最前面的m个数
for (int i = 0; i < m; i++) {
newNums[i] = newNums[newNums.length -(m-i)];
}
//9.遍历新数组
System.out.print("新数组为:");
for (int i = 0; i < nums.length; i++) {
nums[i] = newNums[i];
System.out.print(nums[i] + " ");
}明日练习:定义一个N*N二维数组,从键盘上输入值,找出每行中最大值组成一个一维数组并输出;
大家可以自己写写,明天中午12点我准时发出我的写法哦,明天12点不见不散
一生朋友一生情,一生有你才会赢;千山万水总是情,点个关注行不行!

边栏推荐
猜你喜欢

MySQL8.0.28 installation tutorial

5.合宙Air32F103_LCD_key

R16 Type II量化反馈码本的产生

【遥控器开发基础教程1】疯壳·开源编队无人机-GPIO(遥控器指示灯控制)

Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)

Chapter 10_Index Optimization and Query Optimization

#{}和${}的区别

"Paid paddling" stealthily brushes Brother Ali's face scriptures, challenges bytes three times, and finally achieves positive results

Navicat cannot connect to database Mysql because of WiFi

Nacos source code analysis topic (2) - service registration
随机推荐
mysql使用on duplicate key update批量更新数据
运维理想和现实,你是?
请教各位大佬,如果我代码里面设置了,这个id我在什么地方可以查到呢?连接到mysql cluste
程序员的七夕浪漫时刻
MySQL8--Windows下使用压缩包安装的方法
Heuristic merge, DSU on Tree
PHP WebShell Free Kill
【遥控器开发基础教程1】疯壳·开源编队无人机-GPIO(遥控器指示灯控制)
Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
面试必备!TCP协议经典十五连问!
Reasons and solutions for Invalid bound statement (not found)
【LeetCode】20. Valid parentheses
5.nodejs--跨域、CORS、JSONP 、Proxy
第七周复习
总体写作原则
mysql8.0.28 download and installation detailed tutorial, suitable for win11
就瞎写=感想
MySQL修改最大连接数限制
Chapter 11_Database Design Specifications
5.合宙Air32F103_LCD_key