当前位置:网站首页>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点不见不散
一生朋友一生情,一生有你才会赢;千山万水总是情,点个关注行不行!

边栏推荐
猜你喜欢
随机推荐
基于可逆网络的单一图像超分辨率
生成器知道鉴别器在无条件GANs中应该学习什么
聊聊flink的BoundedOutOfOrdernessTimestampExtractor
Week 304 Dunk
直击程序员面试现场:百度面试官都问了我些啥?
MySQL index optimization in practice
just write blindly = feelings
Heuristic merge, DSU on Tree
PHP WebSehll 后门脚本与检测工具
【遥控器开发基础教程3】疯壳·开源编队无人机-ADC(摇杆控制)
WebShell Feature Value Summary and Detection Tool
Nacos source code analysis topic (2) - service registration
考虑饱和的多智能体系统数据驱动双向一致性
2022年最新一篇文章教你青龙面板拉库,拉取单文件,安装依赖,设置环境变量,解决没有或丢失依赖can‘t find module之保姆教程(附带几十个青龙面板脚本仓库)
合奥科技网络 面试(含参考答案)
输入延迟切换系统的预测镇定控制
* Compare version numbers
5.合宙Air32F103_LCD_key
mysql8.0.28下载和安装详细教程,适配win11
STM32——LCD—TFTLCD原理与配置介绍









