06: integer odd-even sort

Total time limit: 1000ms memory limit: 65536kB
described
sequence of 10 integers given in claim reorder them. Sort Requirements:

1. The odd-first, followed by even-numbered;

2. Press odd descending order;

3. even-numbered in ascending order.

Input
Input line, comprising an integer of 10, separated from each other with a space, the range of each integer greater than or equal to 0 and less than or equal to 100.
Output
according to the requirements of ordering line of output 10 comprising ordering the integers between Number and separated by a space.
Sample input
4,731,311,120,473,498
sample output
4,713,117,304,123,498
source
1873
analytical
thinking hash sorted, a number after determining that the input number does not appear there, there have been several times, and then all of the output number of small to large (several times appeared on many occasions output).
I.e. an array num [x] = k to represent all of the digital input, x k times appeared. Initially all set to 0, indicating that all numbers have not appeared. When all the data inputs of all, the array num on a note of the number of all numbers, required in order to subject all digital output to.

#include<iostream>
#include<cstring>
using namespace std;
int main(){
	//num[x]用来判断数字x有没有出现,出现了几次。 初始时置为0
	int num[105];
	memset(num,0,sizeof(num));
	
	for(int i=0;i<10;i++){
		int x;
		cin>>x;
		num[x]++;
	}
	//先处理奇数,并且由大到小输出 
	for(int i=99;i>=1;i-=2){
		while(num[i]--){
			cout<<i<<' ';
		}
	}
	//再处理偶数,并且由小到大输出 
	for(int i=0;i<=100;i+=2){
		while(num[i]--){
			cout<<i<<' ';
		}
	}
	return 0;
}
Published 36 original articles · won praise 0 · Views 331

Guess you like

Origin blog.csdn.net/weixin_44437496/article/details/104056682