Yitongtong 1177: odd single increasing sequence (father)

1177: odd single increasing sequence


Time limit: 1000 ms Memory limit: 65536 KB
Commits: 14993 Passes: 7526

【Title description】

Given a sequence of positive integers of length N (not greater than 500), take all odd numbers out of them and output them in ascending order.

【Enter】

Line 1 N;

The second line is N positive integers, separated by spaces.

【Output】

Odd sequence output in ascending order, separated by comma between data. The data is guaranteed to have at least one odd number.

【Input example】

10
1 3 2 6 5 4 9 8 7 10

[Sample output]

1,3,5,7,9

Explanation: It seems that insertion sort is more appropriate (because reading a number, you can arrange a number), but lazy, use bubbling. . .

#include<bits/stdc++.h>
using namespace std;
int n;
int a[505];
int main(){
  	freopen("test.in","r",stdin);
  	freopen("test.out","w",stdout);
	cin>>n;
	int k=0;
	int tmp;
	for(int i=1;i<=n;i++){
		cin>>tmp;
		if(tmp%2==1){
			a[++k]=tmp;
			//printf("a[%d]=%d ",k,tmp);
		}
	}
	for(int i=1;i<=k;i++){
		for(int j=1;j<=k-i;j++){
			if(a[j]>a[j+1]){
				swap(a[j],a[j+1]);
			}
		}
	}
	cout<<a[1];
	for(int i=2;i<=k;i++){
		cout<<","<<a[i];
	}
	
	return 0;
}

 

Posted 33 original articles · liked 0 · visits 167

Guess you like

Origin blog.csdn.net/weixin_42790071/article/details/105464228