|
5鱼币
题目HXY得到了一些卡片,这些卡片上标有数字0或5。现在她可以选择其中一些卡片排成一列,使得排出的一列数字组成的数最大,且满足被90整除这个条件。同时这个数不能含有前导0,即0不能作为这串数的首位。如果不能排出这样的数,输出“-1”。
输入格式
第一行,卡片的个数n
第二行,分别给出了这n个数(只能为数字5或0)。
输出格式
仅一行,如果可以排出,则输出这个数。否则输出“-1”。
示例1
输入:
11
5 5 5 5 5 5 5 5 0 5 5
输出:
5555555550
样例2:
4
5 0 5 0
样例输出2:
0
我的代码:- a = int(input())
- b = list(map(int, input().split()))
- if (b.count(5) < 9 and 0 not in b) or 0 not in b:
- print(-1)
- elif b.count(5) < 9:
- print(0)
- else:
- print("555555555" * (b.count(5) // 9) + "0")
复制代码
但明明可以达到效果,却不知道为何不能通过
本帖最后由 qq1151985918 于 2021-6-17 09:16 编辑
你的问题在于你局限于只有1个0,
如果我给出的卡牌是 5 5 5 5 5 5 5 5 0 5 5 0 0 0 0
你再试试看能不能效果相同?
还有,你代码的 的一个 if 条件貌似有些问题
- def fun(length: int, data: str) -> str:
- data = data.split()
- c0 = data.count("0")
- c5 = data.count("5")
- if c0 == 0 or c5 < 9:
- return "-1"
- else:
- return "".join(sorted(data)[::-1][:c5 - c5 % 9] + ["0"] * c0)
- if __name__ == "__main__":
- ## in_length = 11
- ## in_data = "5 5 5 5 5 5 5 5 0 5 5"
- in_length = int(input("请输入卡牌个数:"))
- in_data = input("请输入卡牌组合:")
- while len(in_data.split()) != in_length:
- in_data = input("请重新输入卡牌组合:")
- print(fun(in_length, in_data))
复制代码
|
最佳答案
查看完整内容
你的问题在于你局限于只有1个0,
如果我给出的卡牌是 5 5 5 5 5 5 5 5 0 5 5 0 0 0 0
你再试试看能不能效果相同?
还有,你代码的 的一个 if 条件貌似有些问题
|