zoukankan      html  css  js  c++  java
  • 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:
      • 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].
     
    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
    3 0 1 3 3 5 4 2 3 2 4 6
     
    Sample Output
    301 425 -1
     
    Source
     
     
          算法分析:给你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
    ");
                      continue;
                  }
                  else if(nn%2==1 )
                  {
                      printf("%d
    ", 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
    ");
                   continue;
            }
             if(dd>=n-1)
             {
                 printf("-1
    ");
                   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
    ", mm);
        }
        return 0;
    }
    
  • 相关阅读:
    insert sort O(n2)
    JJ数据
    quick sort O(logn)
    TSQL语句之case when then 多条件判断
    C#使用LitJson解析JSON
    终止线程 Response.End 在Asp.net 里面的正确使用
    TSQL操作MSSQL2008 SQL备份与还原数据库
    C#/.NET 条件合并两个DataTable
    JavaScript学习之一JavaScript浏览器对象模型详解window对象(上)
    跟老邓一起学Windows Phone7开发(一)第一个程序
  • 原文地址:https://www.cnblogs.com/yspworld/p/4052585.html
Copyright © 2011-2022 走看看