当前位置:网站首页>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 4
Topic 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];
}
}
边栏推荐
- STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
- PTA tiantisai l1-078 teacher Ji's return (15 points) detailed explanation
- In Net 6 CS more concise method
- 2/12 didn't learn anything
- Record the pit of NETCORE's memory surge
- 【可调延时网络】基于FPGA的可调延时网络系统verilog开发
- HotSpot VM
- Several important classes in unity
- In depth MySQL transactions, stored procedures and triggers
- Leetcode32 longest valid bracket (dynamic programming difficult problem)
猜你喜欢
Practical development of member management applet 06 introduction to life cycle function and user-defined method
Query the number and size of records in each table in MySQL database
Proof of Stirling formula
About some basic DP -- those things about coins (the basic introduction of DP)
Maxay paper latex template description
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
WPF effect Article 191 box selection listbox
Le compte racine de la base de données MySQL ne peut pas se connecter à distance à la solution
【PSO】基于PSO粒子群优化的物料点货物运输成本最低值计算matlab仿真,包括运输费用、代理人转换费用、运输方式转化费用和时间惩罚费用
Mysql数据库慢sql抓取与分析
随机推荐
C form application of C (27)
Query the number and size of records in each table in MySQL database
User datagram protocol UDP
Maxay paper latex template description
MySQL master-slave replication
绑定在游戏对象上的脚本的执行顺序
[disassembly] a visual air fryer. By the way, analyze the internal circuit
深入浅出node模板解析错误escape is not a function
2328. 网格图中递增路径的数目(记忆化搜索)
Fundamentals of SQL database operation
2/10 parallel search set +bfs+dfs+ shortest path +spfa queue optimization
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
Network security - Security Service Engineer - detailed summary of skill manual (it is recommended to learn and collect)
Several important classes in unity
Solution of storage bar code management system in food industry
How does technology have the ability to solve problems perfectly
Data processing methods - smote series and adasyn
Global and Chinese market of plasma separator 2022-2028: Research Report on technology, participants, trends, market size and share
POI add border
Tips for using dm8huge table