A. Jeff and Digits(cf)
2013-10-05 11:42
507 查看
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
Output
Input
Output
View Code
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
#include <cstdio> #include <iostream> using namespace std; int main() { int n,x; while(~scanf("%d",&n)) { int cnt5 = 0,cnt0 = 0,max = 0; while(n--) { scanf("%d",&x); if (x==5) cnt5++; else cnt0++; if(cnt5%9==0&&cnt5)//能被9整除的数其各位数之和是9的倍数,故5的个数应是9的倍数 { if (cnt5 > max) max = cnt5; } } if (cnt0==0)//能被90整除的数末尾必含0 puts("-1"); else if (!max)//若5的个数小于9个且cnt0!=0,则最大的数为0 puts("0"); else { while(max--) cout<<"5"; while(cnt0--) cout<<"0"; puts(""); } } return 0; }
View Code
相关文章推荐
- 网络中各层协议
- 排序算法--选择排序
- 帝国cms中 调用内容页评论
- 排序算法--冒泡排序
- Android BaseAdapter
- PHP Warning: date() [function.date]: It is not safe to rely on the system's timezone
- Linux挂载卸载光盘&实践
- 汉诺塔问题的算法分析与实现(Java)
- 你手上有50万,你会干什么?
- 判断101-200之间有多少个素数,并输出所有素数。
- C#中的Serializable
- 求最小公约数和最大公倍数
- 通过远程 http API 来控制 lnmp 环境的重启perl脚本
- listView
- pecel
- 服务器编程模型
- 第三章5
- 输入一串字符,判断英文字母,空格,数字及其他字符的个数
- 求解:块级元素的宽度自适应问题
- 说下Fedora下把SpiderMonkey放入Eclipse内编译的过程