当前位置:网站首页>[C language practice - adjust the order of odd and even numbers in the array]
[C language practice - adjust the order of odd and even numbers in the array]
2022-06-09 13:07:00 【Beginners of C language】
Adjust the order of odd and even numbers
Preface
Adjust the order of odd and even numbers in the array , Make all odd numbers precede even numbers .
1、 Do not change element order
- First, adjust the odd numbers to the front of the array
- Then adjust the even numbers to the end of the array
- The sequence of all odd parts remains the same
- The sequence of all even parts remains the same

int main()
{
int a[] = {
1,2,8,9,4,10,3,5,6,0,7 };
int left = 0;
int sz = sizeof(a) / sizeof(a[0]);
int right = sz-1;
int tmp = 0;// Temporary intermediate variable
int num = sz;// Judge the number of times
while (num)
{
if (a[left] % 2 == 0)// here a[left] It's even , Shift
{
//ou = i;// The starting position of the next judgment
tmp = a[left];
for (int j = left; j <= right - 1; j++)
{
a[j] = a[j + 1];
}
a[right] = tmp; // Even numbers at the end of the array
}
else
{
left++;//a[left] Not even numbers , Then judge the next a[left+1]
}
num--;
}
for (int i = 0; i < sz; i++)
{
printf("%d ", a[i]);
}
return 0;
}
The results are as follows :

10
2、 Change the order of elements
- First, adjust the odd numbers to the front of the array
- Then adjust the even numbers to the end of the array
- The sequence of all odd parts is changed
- The sequence of all even parts has changed

void change(int a[], int sz)
{
int left = 0;
int right = sz - 1;
while (left<right)
{
while ((left<right) && (a[left] % 2 == 1))
{
left++;
}
while ((left < right) && (a[right] % 2 == 0))
{
right--;
}
if (left < right)
{
int tmp = a[left];
a[left] = a[right];
a[right] = tmp;
}
}
}
void print(int a[], int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", a[i]);
}
}
int main()
{
int a[] = {
1,2,8,9,4,10,3,5,6,0,7 };
int sz = sizeof(a) / sizeof(a[0]);
change(a, sz);
print(a, sz);
return 0;
}
The results are as follows :

summary
Still need more practice , No matter the code you write is wordy , Still too bad , You have to finish it , Achieve the requirements of the topic , This is the most important step .
The second step is to look at the code written by others , Learn other people's ideas , Write it down and blog , Convenient for self review .
边栏推荐
猜你喜欢
随机推荐
【leetcode周赛记录】第79场双周赛+第295场周赛记录
Who has better performance than CK, ES and redisearch
[STM32] Hal library DAC
AVR与ARM区别以及常用Arduino
数据库day-6
Handlebars template engine usage 2
周博磊《模型可解释性年度进展概述》20200805
Warning: Accessing non-existent property ‘column‘ of module exports inside circular depen
Cache and TLB
【C语言练习——交换两个变量的值】
. Use of net mvc5 rich text [wangeditor5]
Ten recommended shuttle plug-ins
js中const,var,let定义变量的区别
【STM32】HAL库-CRC校验
Core implementation of Kube apiserver scheduler
[STM32] Hal library CRC verification
com.alibaba.fastjson.JSONException: syntax error, pos 1, line 1, column 2测试
fastapi基于pytest的异步测试(tortoise-orm)
数据库day-4
数字化转型的七个错误认知







