zoukankan      html  css  js  c++  java
  • hdu 5055 Bob and math problem (很简单贪心)

    给N个数字(0-9),让你组成一个数。

    要求:1.这个数是奇数    2.这个数没有前导0

    问这个数最大是多少。

    思路&解法:

    N个数字从大到小排序,将最小的奇数与最后一位交换,把剩下前N-1位从大到小排序。输出。(判断第一位是否为0)

    代码:

    #include <cstdio>
    #include <iostream>
    #include <string.h>
    #include <cstdlib>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <cmath>
    #include <map>
    #include <stack>
    using namespace std;
    int const uu[4] = {1,-1,0,0};
    int const vv[4] = {0,0,1,-1};
    typedef long long ll;
    int const maxn = 50005;
    int const inf = 0x3f3f3f3f;
    ll const INF = 0x7fffffffffffffffll;
    double eps = 1e-10;
    double pi = acos(-1.0);
    #define rep(i,s,n) for(int i=(s);i<=(n);++i)
    #define rep2(i,s,n) for(int i=(s);i>=(n);--i)
    #define mem(v,n) memset(v,(n),sizeof(v))
    #define lson l, m, rt<<1
    #define rson m+1, r, rt<<1|1
    
    int n;
    int a[105];
    
    bool cmp(int a,int b){
        return a>b;
    }
    int main(){
        while(scanf("%d",&n)!=EOF){
            rep(i,0,n-1) scanf("%d",&a[i]);
            sort(a,a+n,cmp);
    
            int t1=-1;
            rep2(i,n-1,0) if(a[i]%2==1){
                t1=i;
                break;
            }
            if(t1==-1){
                printf("-1
    ");
                continue;
            }
            swap(a[t1],a[n-1]);
            sort(a,a+n-1,cmp);
            if(a[0]==0){
                printf("-1
    ");
                continue;
            }
            rep(i,0,n-1) printf("%d",a[i]); cout<<endl;
        }
    }
  • 相关阅读:
    bzoj1221
    hdu3377
    bzoj3930
    bzoj3976
    bzoj4237
    fzu1977
    hdu1693
    ural1519
    bzoj1264
    回答自己的提问
  • 原文地址:https://www.cnblogs.com/fish7/p/4000887.html
Copyright © 2011-2022 走看看