HDU —— 5055 Bob and math problem
2014-09-29 10:35
375 查看
题意:给你N个数,每个数的取值范围为[0,9],求这N个数组成的最大奇数,且该奇数不能有前导零。
思路:对N个数进行排序,然后找出最小的奇数的位置,接着从后往前输出,最小奇数放在最后输出。注意:题目虽然很简单,但是有很多细节问题要考虑去全面,比如N=1时有两种情况,即该元素为奇数或不为奇数;当非零元素为1时,此时输出-1;当全为偶数时输出-1。
代码:
思路:对N个数进行排序,然后找出最小的奇数的位置,接着从后往前输出,最小奇数放在最后输出。注意:题目虽然很简单,但是有很多细节问题要考虑去全面,比如N=1时有两种情况,即该元素为奇数或不为奇数;当非零元素为1时,此时输出-1;当全为偶数时输出-1。
代码:
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int main() { int N,K,num[110]; while(scanf("%d",&N)==1) { int flag, sum=0; for(int i = 0;i < N; i++) { scanf("%d",&num[i]); if(num[i]!=0) sum++; //sum记录非零个数 } sort(num,num+N); if(N == 1 && num[0]%2== 1) { printf("%d\n",num[0]); continue; }//当N=1且num[0]为奇数时,直接输出 if((N==1 && num[0]%2 != 1) || sum <= 1) { printf("-1\n"); continue; } //当N=1且num[0]不为奇数时或者非零个数小于1时,输出-1 for(K = 0; K < N; K++) { if(num[K]%2 == 1) { flag = K; break; } } if(K == N) printf("-1\n");//当K==N时,即奇数个数为0,输出-1 else { for(int j=N-1;j >= 0; j--) if(j != flag) printf("%d",num[j]); //在最小奇数位置时跳过 printf("%d\n",num[flag]); } } return 0; }
相关文章推荐
- hdu 5055 Bob and math problem
- hdu 5055 Bob and math problem (很简单贪心)
- HDU 5055 Bob and math problem(贪心)——BestCoder Round #11(div.2)
- HDU 5055 Bob and math problem
- hdu----(5055)Bob and math problem(贪心)
- hdu--5055 Bob and math problem
- HDU 5055 Bob and math problem(结构体)
- hdu 5055 Bob and math problem
- hdu 5055 Bob and math problem
- HDU 5055 Bob and math problem
- HDU 5055 Bob and math problem
- hdu5055——Bob and math problem
- HDU 5055 Bob and math problem
- [5055]Bob and math problem(hdu)
- HDU 5055 Bob and math problem(构造)
- 【CUGBACM15级BC第11场 B】hdu 5055 Bob and math problem
- hdu 5055 Bob and math problem
- HDU 5055 Bob and math problem
- HDU 5055 - Bob and math problem(贪心)
- HDU 5055 Bob and math problem