练习cf352AA. Jeff and Digits
题目如下
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.
题目大意
现有数组由0和5组成,可以用数组的元素组成一个数,能否找出这样一个数可以被90整除。
题目分析
要实现对90的整除,要满足两个条件
- 至少有一个0
- 至少有9个5,且5的个数必须是9的倍数
完整代码
点击查看代码
#include <stdio.h>
int main(){
int n;
scanf("%d", &n);
int x, count5 = 0, count0 = 0;
for(int i = 0; i < n; i++) {
scanf("%d", &x);
if(x == 5){
count5++;
}else{
count0++;
}
}
if(count0 == 0){
printf("-1\n"); // 没有0
}else if(count5 < 9){
printf("0\n");
}else{
int use = count5 / 9 * 9; //最多能用的5的个数
for(int i = 0; i < use; i++){
printf("5");
}
for(int i = 0; i < count0; i++){
printf("0");
}
printf("\n");
}
return 0;
}

浙公网安备 33010602011771号