题目描述
给定一个长度为 N
(不大于 500
)的正整数序列,请将其中的所有奇数取出,并按升序输出。
输入格式
第 1
行为 N
;第 2
行为 N
个正整数,其间用空格间隔。
输出格式
增序输出的奇数序列,数据之间以逗号间隔。数据保证至少有一个奇数。
输入样例
10
1 3 2 6 5 4 9 8 7 10
输出样例
1,3,5,7,9
测试点没有全过
#include<bits/stdc++.h> using namespace std; map<int,int>mp; int main(){ int n,f=0; cin>>n; while(n--){ int x; cin>>x; mp[x]++; } for(auto it=mp.begin();it!=mp.end();it++){ if(f++)cout<<','; cout<<it->first; } return 0; }
满分代码
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int a[n],f=0,cnt=0; for(int i=0;i<n;i++){ int x; cin>>x; if(x%2) a[cnt++]=x; } sort(a,a+cnt); for(int i=0;i<cnt;i++){ if(f++)cout<<','; cout<<a[i]; } return 0; }