标签:class code color int strong string
输入10个整数,彼此以空格分隔。重新排序以后输出(也按空格分隔),要求:
1.先输出其中的奇数,并按从大到小排列;
2.然后输出其中的偶数,并按从小到大排列。
任意排序的10个整数(0~100),彼此以空格分隔。
可能有多组测试数据,对于每组数据,按照要求排序后输出,由空格分隔。
4 7 3 13 11 12 0 47 34 98
47 13 11 7 3 0 4 12 34 98
1.
测试数据可能有很多组,请使用while(cin>>a[0]>>a[1]>>...>>a[9])类似的做法来实现;
2.
输入数据随机,有可能相等。
#include
"iostream"using namespace std;int
main(int argc, char* argv[]){ int a[10],temp1[10],temp2[10]; int i,j,flag1,flag2; while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]>>a[8]>>a[9]){ flag1=0; flag2=0; for(i=0;i<=9;i++){ if(a[i]%2!=0){ temp1[flag1]=a[i]; flag1++; } else{ temp2[flag2]=a[i]; flag2++; } } for(i=0;i<=flag1-2;i++) for(j=i;j<=flag1-1;j++){ if(temp1[j]>temp1[i]){ int temp; temp=temp1[i]; temp1[i]=temp1[j]; temp1[j]=temp; } } for(i=0;i<=flag2-2;i++) for(j=i;j<=flag2-1;j++){ if(temp2[j]<temp2[i]){ int temp; temp=temp2[i]; temp2[i]=temp2[j]; temp2[j]=temp; } } for(i=0;i<=flag1-1;i++){ if(i!=flag1-1)
cout<<temp1[i]<<"
"; else if((i=flag1-1)&&(flag2==0))
//要注意的是此处:如果全是奇数的情况,那么最后一个空格就不要输出了。 cout<<temp1[i]; else cout<<temp1[i]<<"
"; } for(i=0;i<=flag2-1;i++) if(i!=flag2-1) cout<<temp2[i]<<"
"; else cout<<temp2[i]; cout<<endl; } return 0;} Problem: 1117 Language: C++ Result: Accepted Time:190 ms Memory:1520 kb 题目1117:整数奇偶排序 (2008年北京大学图形实验室计算机研究生机试真题),布布扣,bubuko.com
题目1117:整数奇偶排序 (2008年北京大学图形实验室计算机研究生机试真题)
标签:class code color int strong string
原文地址:http://www.cnblogs.com/Murcielago/p/3704157.html