A
来源:互联网 发布:枕头什么牌子好 知乎 编辑:程序博客网 时间:2024/05/17 03:48
Description
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 airepresents 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 Input
45 0 5 0
0
115 5 5 5 5 5 5 5 0 5 5
5555555550
Hint
In the first test you can make only one number that is a multiple of 90 — 0.
In the second test you can make number 5555555550, it is a multiple of 90.
解题思路:要想被9整除,应该是9的倍数个5,被90整除后面再加0就行
代码:
#include<iostream>using namespace std;int main(){ int n,i,a; while(cin>>n) { int num0=0; int num5=0; for(i=0;i<n;i++) { cin>>a; if(a==0) num0++; if(a==5) num5++; } if(num0==0) cout<<-1<<endl; else { if(num5<9) cout<<0<<endl; else if(num5>=9) { for(i=0;i<(num5/9)*9;i++) cout<<5; for(i=0;i<num0;i++) cout<<0; cout<<endl; } } } return 0;}
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- A
- A*
- a
- A
- a
- js中 javascript:void(0) 用法详解
- Windows10+DroneKit+Python2.7
- 编写一个程序,将浮点数转换成人名币读法字符串(学习记录)
- Qt动态库和静态库的加载及使用
- 转载的哦。一位资深程序员大牛给予Java初学者的学习建议
- A
- maven入门4-与eclipse整合
- hdu1078(dp+dfs)
- DS1302+AT24C02+按键数码管显示程序 (原创)
- events.js:160 throw er; // Unhandled 'error' event ^ Error: write after end at Serv
- select取值
- Spring 核心技术——AOP 简介
- 操作系统常见面试题(二)
- Rxjava Subject分析