当前位置:网站首页>1008 circular right shift of array elements (20 points)
1008 circular right shift of array elements (20 points)
2022-07-06 04:14:00 【White phoenix leans on the sword and returns】
An array A There is N(>0) It's an integer , On the premise that no other array is allowed , Loop each integer to the right M(≥0) A place , the A The data in is from (A0A1⋯AN−1) Transformation for (AN−M⋯AN−1A0A1⋯AN−M−1)( Last M The number loop moves to the front of M A place ). If you need to consider the program to move data as little as possible , How to design a way to move ?
Input format :
Each input contains a test case , The first 1 Line input N(1≤N≤100) and M(≥0); The first 2 Line input N It's an integer , Space between .
Output format :
In a row, the output loop shifts to the right M Sequence of integers after bits , Space between , There must be no extra spaces at the end of the sequence .
sample input :
6 2
1 2 3 4 5 6
sample output :
5 6 1 2 3 4Topic analysis :
1. Use an intermediate variable to store the last item of the array
2. Let the last item of the array -1 Items move backward in turn
3. Let the first item = Intermediate variable
4. Output
The code is as follows :
C Language :
#include <stdio.h>
int main() {
int a[101];
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < m; i++) {
int t = a[n - 1];
for (int j = n - 2; j >= 0; j--) {
a[j + 1] = a[j];
}
a[0] = t;
}
printf("%d", a[0]);
for (int i = 1; i < n; i++) {
printf(" %d", a[i]);
}
}C++:
#include <iostream>
using namespace std;
int main() {
int a[101];
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
int t = a[n - 1];
for (int j = n - 2; j >= 0; j--) {
a[j + 1] = a[j];
}
a[0] = t;
}
cout << a[0];
for (int i = 1; i < n; i++) {
cout << " " << a[i];
}
}边栏推荐
- 10个 Istio 流量管理 最常用的例子,你知道几个?
- QML和QWidget混合开发(初探)
- Solution of storage bar code management system in food industry
- C form application of C (27)
- 10 exemples les plus courants de gestion du trafic istio, que savez - vous?
- Cross domain and jsonp details
- Comprehensive ability evaluation system
- Benefits of automated testing
- 2/12 didn't learn anything
- 1291_Xshell日志中增加时间戳的功能
猜你喜欢

ESP32_ FreeRTOS_ Arduino_ 1_ Create task

DM8 archive log file manual switching

C (thirty) C combobox listview TreeView

查询mysql数据库中各表记录数大小

During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input

How many of the 10 most common examples of istio traffic management do you know?

lora网关以太网传输

Esp32 (based on Arduino) connects the mqtt server of emqx to upload information and command control

R note prophet

Stack and queue
随机推荐
图应用详解
[Zhao Yuqiang] deploy kubernetes cluster with binary package
Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
【FPGA教程案例11】基于vivado核的除法器设计与实现
Execution order of scripts bound to game objects
电脑钉钉怎么调整声音
MySql数据库root账户无法远程登陆解决办法
P2102 地砖铺设(dfs&贪心)
Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
Query the number and size of records in each table in MySQL database
Deep learning framework installation (tensorflow & pytorch & paddlepaddle)
Brief tutorial for soft exam system architecture designer | general catalog
C form application of C (27)
Lombok原理和同时使⽤@Data和@Builder 的坑
[disassembly] a visual air fryer. By the way, analyze the internal circuit
2/12 didn't learn anything
Global and Chinese markets for fire resistant conveyor belts 2022-2028: Research Report on technology, participants, trends, market size and share
Solution to the problem that the root account of MySQL database cannot be logged in remotely
1291_Xshell日志中增加时间戳的功能
[tomato assistant installation]