HDU 5055 Bob and math problem
2014-10-26 19:26
295 查看
Bob and math problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 961 Accepted Submission(s): 368
[align=left]Problem Description[/align]
Recently, Bob has been thinking about a math problem.
There are N Digits, each digit is between 0 and 9. You need to use this N Digits to constitute an Integer.
This Integer needs to satisfy the following conditions:
1. must be an odd Integer.
2. there is no leading zero.
3. find the biggest one which is satisfied 1, 2.
Example:
There are three Digits: 0, 1, 3. It can constitute six number of
Integers. Only "301", "103" is legal, while "130", "310", "013", "031"
is illegal. The biggest one of odd Integer is "301".
[align=left]Input[/align]
There are multiple test cases. Please process till EOF.
Each case starts with a line containing an integer N ( 1 <= N <= 100 ).
The second line contains N Digits which indicate the digit [Math Processing Error].
[align=left]Output[/align]
The
output of each test case of a line. If you can constitute an Integer
which is satisfied above conditions, please output the biggest one.
Otherwise, output "-1" instead.
[align=left]Sample Input[/align]
3
0 1 3
3
5 4 2
3
2 4 6
[align=left]Sample Output[/align]
301
425
-1
[align=left]Source[/align]
BestCoder Round #11 (Div. 2)
算法分析:给你n个数,让着n个数组成 符合要求的数字输出,组不出来的话就输出-1.
条件1. 组成的是 奇数。
条件2. 没有前导0。
条件3.在满足前两个条件的基础上保证数最大!
算法实现:1. 检查这n个数里 有没有奇数, 如果没有 直接输出-1 。
2. 检查0的个数, 如果0的个数>=n-1,这样组出来的数 要么具有前导0,要么不是奇数,直接输出-1.
3. 将最小的奇数 放在最后输出, 将其余的数字 按从大到小的顺序输出即可。
4. 如果n==1,也就是说,只输入一个数,奇数的话就直接输出,否则输出-1。
#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <algorithm> using namespace std; int main() { int n, mm, nn; int a[101]; int ff[101]; int i, j; int ee, oo, dd; while(scanf("%d", &n)!=EOF) { if(n==1) { scanf("%d", &nn); if(nn%2==0) { printf("-1\n"); continue; } else if(nn%2==1 ) { printf("%d\n", nn); continue; } } ee=0; oo=0; dd=0; mm=999999; for(i=0; i<n; i++) { scanf("%d", &a[i] ); if(a[i]%2==1) { oo++; //奇数 if( a[i]<mm ) { mm=a[i]; } } else if(a[i]%2==0) { ee++; if(a[i]==0) dd++; //0的个数 } } if(oo==0) { printf("-1\n"); continue; } if(dd>=n-1) { printf("-1\n"); continue; } memset(ff, 0, sizeof(ff)); sort(a, a+n); for(i=0; i<n; i++) { if(a[i]==mm) { ff[i]=1; break; } } for(i=n-1; i>=0; i--) { if(ff[i]==0) printf("%d", a[i] ); } printf("%d\n", mm); } return 0; }
相关文章推荐
- hdu 5055 Bob and math problem
- hdu5055——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
- [5055]Bob and math problem(hdu)
- 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(模拟)
- 【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