PTA adjusts the array so that all odd numbers are in front of even numbers and the order of other numbers remains unchanged.

Table of contents

topic:

source code:

Idea 1

Idea 2


topic:

Enter a number string with a length of no more than 10, and adjust the array so that all odd numbers are in front of even numbers and the order of other numbers remains unchanged.

Input format:

For example, enter "0123456789"

Output format:

Output "1357902468"

Input example:

Give a set of inputs here. For example:

0123456789

Output sample:

The corresponding output is given here. For example:

1357902468

source code:

Idea 1

#include<stdio.h>
#include <string.h>
int main()
{
	char arr1[11],arr2[11];
	int t;
	gets(arr1);
	int p;
	int num=0;
	p=strlen(arr1);
	for(int i=0;i<p;i++)
	{
		t=arr1[i]-'0';
		if(t%2!=0)printf("%d",t);
		else
		{
			arr2[num]=arr1[i];
			num++;
		}
	}
	for(int i=0;i<num;i++)
	{
		printf("%c",arr2[i]);
	}
}

Idea 2

#include<stdio.h>
#include<string.h>
int main()
{
	char arr1[11];
	char arr2[11];
	int arr3[11];
 	int i=0,k=0,sum=0,t=0;
    gets(arr1);
    i=strlen(arr1);
 	for (k = 0; k < i; k++)
 	{
 		if (arr1[k] % 2 == 1)
  		{
   			arr2[sum] = arr1[k];
   			sum++;
  		}
 	}
 	for (k = 0; k < i; k++)
 	{
		if (arr1[k] % 2 == 0)
		{
			arr2[sum] = arr1[k];
			sum++;
		}
	}
	for (k = 0; k < i; k++)
	{
		printf("%c", arr2[k]);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_60960413/article/details/122149472