• 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;
        }
    }
  • 相关阅读:
    基于《Hadoop权威指南 第三版》在Windows搭建Hadoop环境及运行第一个例子
    使用java发送HTTP请求
    关于centOS7的一些笔记
    关于netty的多个handler链式模式
    关于netty的简单实现
    EF---延迟加载技术
    Restful Api 最佳实践
    FlaskWeb开发
    python多线程/多进程
    Python网络编程
  • 原文地址:https://www.cnblogs.com/fish7/p/4000887.html
Copyright © 2020-2023  润新知