当前位置:网站首页>[c language] shift elements after sorting elements of an array
[c language] shift elements after sorting elements of an array
2022-06-11 18:18:00 【Dancing Pierre】
[C Language ] After sorting the elements of an array, shift the elements
1、 subject
Xiao Ke likes to study arrays recently , He found a new problem : How to sort the elements of an array and then translate the elements ?
Set the given original array as :4 2 3 1 5 8 7 10 6 9
First enter 1, Sort in ascending order 1 2 3 4 5 6 7 8 9 10
Input again 3, Then put the front 3 Elements are translated to the back of the array , The final array element is 4 5 6 7 8 9 10 1 2 3
Examples 1:
Input :
1
3
Output :
1 2 3 4 5 6 7 8 9 10
4 5 6 7 8 9 10 1 2 3
Examples 2:
Input :
2
5
Output :
10 9 8 7 6 5 4 3 2 1
5 4 3 2 1 10 9 8 7 6
2、 Complete code
#define _CRT_SECURE_NO_WARNINGS // Visual Studio The version is too new and needs to be added
#include <stdio.h>
// Ascending
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;
}
// Descending
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;
}
// Displacement
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、 Screenshot


边栏推荐
猜你喜欢

mysql8安装,navicat安装,sqli-labs搭建

RadioGroup动态添加RadioButton

TR-069 protocol introduction
![[C语言]用结构体把输入的指定分数范围内的学生输出](/img/40/cbd7fe5aafbaeb6237e6d257455e5e.png)
[C语言]用结构体把输入的指定分数范围内的学生输出

【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits

“LSTM之父”新作:一种新方法,迈向自我修正的神经网络

SISO decoder for a general (n,n-1) SPC code(補充章節3)

Ctfhub SQL Boolean blind annotation
![Spring 2021 daily question [week3 not finished]](/img/a4/72f2235d014613d26be0fc3524d236.jpg)
Spring 2021 daily question [week3 not finished]
![[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (I)](/img/3e/efce1cff81abf395ffc24ac57b05d7.png)
[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (I)
随机推荐
网络安全威胁情报体系
Global and Chinese markets for private internet access 2022-2028: Research Report on technology, participants, trends, market size and share
Hello go (XIII). Go language common standard library III
Various poses for text modification using sed
Sa-Token 单点登录 SSO模式二 URL重定向传播会话示例
H. 264 concept
TR-069协议介绍
General terms in security field
Système d'information sur les menaces à la sécurité des réseaux
Experiment 3: design and verify all operations represented by linear table sequence on the computer
File class learning
Retrofit source code analysis
The tle6389 step-down DC-DC switch controller has high efficiency in the whole load range of 1mA to 2.5A - keshijin mall
[Golang]力扣Leetcode - 292. Nim 游戏(数学)
vim常用命令
Reading summary of nacos2.x source code
[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (I)
Talking about telework | community essay solicitation
Feign 共享登录信息进行请求
神经网络与深度学习-2- 机器学习简单示例-PyTorch