当前位置:网站首页>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];
}
}边栏推荐
- Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
- How does technology have the ability to solve problems perfectly
- MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
- MySql数据库root账户无法远程登陆解决办法
- /usr/bin/gzip: 1: ELF: not found/usr/bin/gzip: 3: : not found/usr/bin/gzip: 4: Syntax error:
- C form application of C (27)
- Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
- pd. to_ numeric
- Stable Huawei micro certification, stable Huawei cloud database service practice
- Cross domain and jsonp details
猜你喜欢

Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization

关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解

电脑钉钉怎么调整声音

10个 Istio 流量管理 最常用的例子,你知道几个?

Comprehensive ability evaluation system

Lora gateway Ethernet transmission

CertBot 更新证书失败解决

C mouse event and keyboard event of C (XXVIII)

记一次excel XXE漏洞

Slow SQL fetching and analysis of MySQL database
随机推荐
图应用详解
[FPGA tutorial case 12] design and implementation of complex multiplier based on vivado core
[introduction to Django] 11 web page associated MySQL single field table (add, modify, delete)
PTA tiantisai l1-078 teacher Ji's return (15 points) detailed explanation
[FPGA tutorial case 11] design and implementation of divider based on vivado core
IDEA编译JSP页面生成的class文件路径
P3500 [POI2010]TES-Intelligence Test(二分&离线)
2/13 review Backpack + monotonic queue variant
Chinese brand hybrid technology: there is no best technical route, only better products
[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos
AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
使用JS完成一个LRU缓存
MySql数据库root账户无法远程登陆解决办法
Custom event of C (31)
软考 系统架构设计师 简明教程 | 总目录
Recommendation system (IX) PNN model (product based neural networks)
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
[adjustable delay network] development of FPGA based adjustable delay network system Verilog
1291_Xshell日志中增加时间戳的功能
VPP性能测试