zoukankan      html  css  js  c++  java
  • A. Jeff and Digits(cf)

    A. Jeff and Digits
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Jeff's got n cards, each card contains either digit 0, or digit 5. Jeff can choose several cards and put them in a line so that he gets some number. What is the largest possible number divisible by 90 Jeff can make from the cards he's got?

    Jeff must make the number without leading zero. At that, we assume that number 0 doesn't contain any leading zeroes. Jeff doesn't have to use all the cards.

    Input

    The first line contains integer n (1 ≤ n ≤ 103). The next line contains n integers a1, a2, ..., an (ai = 0 or ai = 5). Number ai represents the digit that is written on the i-th card.

    Output

    In a single line print the answer to the problem — the maximum number, divisible by 90. If you can't make any divisible by 90 number from the cards, print -1.

    Sample test(s)
    Input
    4
    5 0 5 0
    Output
    0
    Input
    11
    5 5 5 5 5 5 5 5 0 5 5
    Output
    5555555550
     1 #include <cstdio>
     2 #include <iostream>
     3 using namespace std;
     4 int main()
     5 {
     6     int n,x;
     7     while(~scanf("%d",&n))
     8     {
     9         int cnt5 = 0,cnt0 = 0,max = 0;
    10         while(n--)
    11         {
    12             scanf("%d",&x);
    13             if (x==5)
    14                 cnt5++;
    15             else
    16                 cnt0++;
    17             if(cnt5%9==0&&cnt5)//能被9整除的数其各位数之和是9的倍数,故5的个数应是9的倍数
    18             {
    19                 if (cnt5 > max)
    20                     max = cnt5;
    21             }
    22         }
    23         if (cnt0==0)//能被90整除的数末尾必含0
    24             puts("-1");
    25         else if (!max)//若5的个数小于9个且cnt0!=0,则最大的数为0
    26             puts("0");
    27         else
    28         {
    29             while(max--)
    30                 cout<<"5";
    31             while(cnt0--)
    32                 cout<<"0";
    33             puts("");
    34         }
    35 
    36     }
    37     return 0;
    38 }
    View Code
  • 相关阅读:
    应用量化时代 | 微服务架构的服务治理之路
    API网关——Kong实践分享
    容器云未来:Kubernetes、Istio 和 Knative
    微服务网关实战——Spring Cloud Gateway
    服务迁移之路 | Spring Cloud向Service Mesh转变
    基于事件驱动机制,在Service Mesh中进行消息传递的探讨
    MSMQ 和 MQTT
    MQTT和WebSocket
    NetCore WebSocket 即时通讯示例
    .NET 即时通信,WebSocket服务端实例
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3352250.html
Copyright © 2011-2022 走看看