当前位置:网站首页>[C语言]对一个数组的元素排序后平移元素
[C语言]对一个数组的元素排序后平移元素
2022-06-11 17:59:00 【跳舞的皮埃尔】
1、题目
小科最近喜欢研究数组,他发现了一个新问题:如何对一个数组的元素排序后平移元素?
设给定原数组为:4 2 3 1 5 8 7 10 6 9
先输入1,则升序排序 1 2 3 4 5 6 7 8 9 10
再输入3,则把前面 3 个元素平移到数组后面,最终数组元素为 4 5 6 7 8 9 10 1 2 3
样例1:
输入:
1
3
输出:
1 2 3 4 5 6 7 8 9 10
4 5 6 7 8 9 10 1 2 3
样例2:
输入:
2
5
输出:
10 9 8 7 6 5 4 3 2 1
5 4 3 2 1 10 9 8 7 6
2、完整代码
#define _CRT_SECURE_NO_WARNINGS // Visual Studio版本太新需加上
#include <stdio.h>
// 升序
int Increase(int a[], int n) {
for (int i = 0; i < n - 1; i++)
{
if (a[i] > a[i + 1])
{
int temp;
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
return a;
}
// 降序
int Reduce(int a[], int n) {
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1 - i; j++)
if (a[j] < a[j + 1])
{
int temp;
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
return a;
}
// 位移
int MoveElements(int a[], int n, int k)
{
for (int i = 0; i < k; i++)
{
int m = k - 1 - i;
int j = m + 1;
while (j < n - i)
{
a[m] += a[j];
a[j] = a[m] - a[j];
a[m] -= a[j];
m++;
j++;
}
}
return a;
}
int main() {
int a, b, i, j;
int x[] = {
4 ,2 ,3 ,1 ,5 ,8 ,7 ,10, 6, 9 };
scanf("%d", &a);
if (a == 1) {
for (j = 10; j >= 1; j--)
{
Increase(x, 10);
}
for (i = 0; i < 10; i++)
{
printf("%d ", x[i]);
}
printf("\n");
}
else if (a == 2) {
Reduce(x, 10);
for (i = 0; i < 10; i++) {
printf("%d ", x[i]);
}
printf("\n");
}
scanf("%d", &b);
MoveElements(x, 10, b);
for (i = 0; i < 10; i++)
{
printf("%d ", x[i]);
}
}
3、截图


边栏推荐
- LeetCode_ Prefix tree_ Medium_ 208. implement trie (prefix tree)
- How to learn and self-study
- 夜神安装apk,以及bp代理
- Jsfinder, wafw00f installation, nmap configuration (msvcr120.dll file is missing)
- Spring 2021 daily question [week3 not finished]
- "College entrance examination" volume sent to the big model: 442 people put forward 204 tasks to the big model, led by Google
- 6-8 reading and writing of structured files 1
- 【先收藏,早晚用得到】100个Flink高频面试题系列(三)
- EditText 金额限制
- vulhub
猜你喜欢
![Spring 2021 daily question [week6 not finished]](/img/32/74cff6f057ef9a7941fd6a41dc6635.jpg)
Spring 2021 daily question [week6 not finished]

SISO decoder for min sum (supplementary Chapter 2)

Database lock and transaction isolation level
![Winter vacation daily question (improvement group) [end of week 4]](/img/67/89b5164712d8c4eb319b9266dd4b91.jpg)
Winter vacation daily question (improvement group) [end of week 4]

【先收藏,早晚用得到】100个Flink高频面试题系列(三)

【新手上路常见问答】关于项目管理

Ffmpeg parity field frame interlace progressive command and code processing

SISO Decoder for Repetition(补充章节4)

The tle6389 step-down DC-DC switch controller has high efficiency in the whole load range of 1mA to 2.5A - keshijin mall

网络和并发编程常见面试题
随机推荐
[document operation] of ACM
ISCSI详解(四)——ISCSI服务端配置实战
Experiment 2: write a program and verify that the linear table sequence represents all operations
Ffmpeg hardware codec NVIDIA GPU
"College entrance examination" volume sent to the big model: 442 people put forward 204 tasks to the big model, led by Google
SISO Decoder for SPC (补充章节1)
关于keil中,while循环条件不成立却无法跳出的问题
[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (II)
7-2 h0107. Pig-Latin
How to learn and self-study
6-1 how many words are needed to form a sentence?
viso的常见操作
NR LDPC 打孔-punctured
Ctfhub SQL Boolean blind annotation
剑指 Offer(第 2 版)
Ffmpeg parity field frame interlace progressive command and code processing
Why is the UDP stream set to 1316 bytes
SISO decoder for a general (n,n-1) SPC code(补充章节3)
SQL语句当查询条件为空时默认查询全部数据,不为空是则按照条件进行查询
SQL报错注入1