鱼C论坛

 找回密码
 立即注册
查看: 3555|回复: 5

[已解决]HYX玩卡牌

[复制链接]
发表于 2021-6-16 23:36:41 | 显示全部楼层 |阅读模式
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

我的代码:
  1. a = int(input())
  2. b = list(map(int, input().split()))
  3. if (b.count(5) < 9 and 0 not in b) or 0 not in b:
  4.     print(-1)
  5. elif b.count(5) < 9:
  6.     print(0)
  7. else:
  8.     print("555555555" * (b.count(5) // 9) + "0")
复制代码

但明明可以达到效果,却不知道为何不能通过
最佳答案
2021-6-16 23:36:42
本帖最后由 qq1151985918 于 2021-6-17 09:16 编辑

你的问题在于你局限于只有1个0,
如果我给出的卡牌是 5 5 5 5 5 5 5 5 0 5 5 0 0 0 0
你再试试看能不能效果相同?
还有,你代码的 的一个 if 条件貌似有些问题
  1. def fun(length: int, data: str) -> str:
  2.     data = data.split()
  3.     c0 = data.count("0")
  4.     c5 = data.count("5")
  5.     if c0 == 0 or c5 < 9:
  6.         return "-1"
  7.     else:
  8.         return "".join(sorted(data)[::-1][:c5 - c5 % 9] + ["0"] * c0)


  9. if __name__ == "__main__":
  10. ##    in_length = 11
  11. ##    in_data = "5 5 5 5 5 5 5 5 0 5 5"
  12.     in_length = int(input("请输入卡牌个数:"))
  13.     in_data = input("请输入卡牌组合:")
  14.     while len(in_data.split()) != in_length:
  15.         in_data = input("请重新输入卡牌组合:")
  16.     print(fun(in_length, in_data))
复制代码

最佳答案

查看完整内容

你的问题在于你局限于只有1个0, 如果我给出的卡牌是 5 5 5 5 5 5 5 5 0 5 5 0 0 0 0 你再试试看能不能效果相同? 还有,你代码的 的一个 if 条件貌似有些问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-16 23:36:42 | 显示全部楼层    本楼为最佳答案   
本帖最后由 qq1151985918 于 2021-6-17 09:16 编辑

你的问题在于你局限于只有1个0,
如果我给出的卡牌是 5 5 5 5 5 5 5 5 0 5 5 0 0 0 0
你再试试看能不能效果相同?
还有,你代码的 的一个 if 条件貌似有些问题
  1. def fun(length: int, data: str) -> str:
  2.     data = data.split()
  3.     c0 = data.count("0")
  4.     c5 = data.count("5")
  5.     if c0 == 0 or c5 < 9:
  6.         return "-1"
  7.     else:
  8.         return "".join(sorted(data)[::-1][:c5 - c5 % 9] + ["0"] * c0)


  9. if __name__ == "__main__":
  10. ##    in_length = 11
  11. ##    in_data = "5 5 5 5 5 5 5 5 0 5 5"
  12.     in_length = int(input("请输入卡牌个数:"))
  13.     in_data = input("请输入卡牌组合:")
  14.     while len(in_data.split()) != in_length:
  15.         in_data = input("请重新输入卡牌组合:")
  16.     print(fun(in_length, in_data))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-17 06:59:43 From FishC Mobile | 显示全部楼层
样例2不得输出-1?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-17 07:47:08 From FishC Mobile | 显示全部楼层
这题先决条件就是至少得有一个0   5的和是9的倍数,也就是说9个5  或18个5或27个5等等
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-17 09:11:47 | 显示全部楼层
本帖最后由 阿奇_o 于 2021-6-17 10:55 编辑

题目有点那个。。
  1. n = input()
  2. ls = list(map(int, input().split()))

  3. if int(n) > 9 and ls.count(5) >= 9 and 0 in ls:

  4.     s = '5' * 9 * (ls.count(5)//9)  # 除去末位的0,最大值,必然是9*N倍个5

  5.     if int(s+'0') % 90 == 0:
  6.         print(int(s+'0'))
  7.     else:
  8.         print(-1)  
  9. else:
  10.     print(0)  # 特例0的情况
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-26 21:03:36 | 显示全部楼层
qq1151985918 发表于 2021-6-16 23:36
你的问题在于你局限于只有1个0,
如果我给出的卡牌是 5 5 5 5 5 5 5 5 0 5 5 0 0 0 0
你再试试看能不能 ...

谢谢大佬的指教!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 06:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表