zoukankan      html  css  js  c++  java
  • 51Nod 1433 0和5

    小K手中有n张牌,每张牌上有一个一位数的数,这个字数不是0就是5。小K从这些牌在抽出任意张(不能抽0张),排成一行这样就组成了一个数。使得这个数尽可能大,而且可以被90整除。

    注意:

    1.这个数没有前导0,

    2.小K不需要使用所有的牌。

    Input
    每个测试数据输入共2行。
    第一行给出一个n,表示n张牌。(1<=n<=1000)
    第二行给出n个整数a[0],a[1],a[2],…,a[n-1] (a[i]是0或5 ) 表示牌上的数字。
    Output
    共一行,表示由所给牌组成的可以被90整除的最大的数,如果没有答案则输出”-1”(没有引号)
    Input示例
    4
    5 0 5 0
    Output示例
    0

    题解:能被9整除的数,各数位之和一定为9的倍数;能被90整除的数,一定是在满足能被9整除的数的基础上加上一个0(不能是前导0)。 
    所以,这里需要判断是否有0,还需要判断5的个数,如果存在这个最大的数,那么最后输出的数,前缀一定是连着n个5(n需要是9的倍数,
    n尽量大),后缀是尽可能多的0(最低一个)
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 using namespace std;
    13 #define lowbit(x) (x&(-x))
    14 #define max(x,y) (x>y?x:y)
    15 #define min(x,y) (x<y?x:y)
    16 #define MAX 100000000000000000
    17 #define MOD 1000000007
    18 #define pi acos(-1.0)
    19 #define ei exp(1)
    20 #define PI 3.141592653589793238462
    21 #define INF 0x3f3f3f3f3f
    22 #define mem(a) (memset(a,0,sizeof(a)))
    23 typedef long long ll;
    24 const int N=1005;
    25 const int mod=1e9+7;
    26 int a[N];
    27 int main()
    28 {
    29     int n;
    30     cin>>n;
    31     int k,num_0=0,num_5=0;
    32     for(int i=0;i<n;i++){
    33         cin>>k;
    34         if(k) num_5++;
    35         else num_0++;
    36     }
    37     bool flag=num_0>0?false:true;
    38     if(flag) cout<<-1<<endl;
    39     else {
    40         bool tag=num_5>=9?false:true;
    41         if(tag) cout<<0<<endl;
    42         else {
    43             int times=num_5/9*9;
    44             for(int i=0;i<times;i++)
    45                 cout<<5;
    46             for(int i=0;i<num_0;i++)
    47                 cout<<0;
    48             cout<<endl;
    49         }
    50     }
    51     return 0;
    52 }
     
  • 相关阅读:
    ASCII 说明
    用GDB调试程序
    手把手教你使用Matplotlib绘图|实战
    什么!Python还能帮你找老婆?
    词云图的几种制作方法评测,你pick哪款
    我常用的10个Python实用小Trick
    爬虫代码详解Python多线程、多进程、协程
    [转载] tomcat集群基于redis共享session解决方案
    集群/分布式环境下5种session处理策略
    java7特性之 try-with-resources
  • 原文地址:https://www.cnblogs.com/wydxry/p/7261769.html
Copyright © 2011-2022 走看看