首页 > 代码库 > HDU 5055 Bob and math problem
HDU 5055 Bob and math problem
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
Problem Description
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:
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".
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".
Input
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].
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].
Output
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.
Sample Input
30 1 335 4 232 4 6
Sample Output
301425-1
Source
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。