JeffandDigits
来源:互联网 发布:js导出excel 数字格式 编辑:程序博客网 时间:2024/06/05 20:06
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 containsn integers a1,a2,..., an (ai = 0 or ai = 5). Numberai represents the digit that is written on thei-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
Sample Output
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.
#include<algorithm>
using namespace std;
int b[10000];
int main()
{
long long n,i,s,t,l;
while(cin>>n)
{
s=0;l=0;
for(i=0;i<n;i++)
cin>>b[i];
sort(b,b+n);
if(b[0])
cout<<"-1"<<endl;
else
{
for(i=0;i<n;i++)
{
if(b[i])
s++;
else
l++;
}
t=s/9;
for(i=1;i<=t*9;i++)
cout<<"5";
if(t==0)
cout<<"0";
else
for(i=1;i<=l;i++)
cout<<"0";
cout<<endl;
}
}
}
9的倍数个5组合才能被9整除t=0
是只能输出一个0
- JeffandDigits
- JeffandDigits
- Hive安装&初体验
- hdu 4282
- select、poll、epoll之间的区别总结[整理]
- 学习Linux的几点忠告
- 微信开发 接口配置失败的坑新手注意
- JeffandDigits
- mybatis学习(一)
- I/O多路复用之select系统调用
- Windows7 64位 安装tensorflow
- jQuery中一些必须要知道的知识点总结--20个(上)
- Mybatis之入门Helloworld程序
- TCP/IP四层模型和OSI七层模型的概念
- jvm_demo_TestThread
- net.sf jsonobject将string转json时报错