鱼C论坛

 找回密码
 立即注册
查看: 1887|回复: 12

哪位大佬教教我这俩个题目怎么做?想了几个小时了想不明白

[复制链接]
发表于 2022-3-6 21:51:51 | 显示全部楼层 |阅读模式
20鱼币
-733828ffdf4aa4c9.jpg eef274dba71a40d.jpg

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-6 22:29:15 | 显示全部楼层
可以放一下链接吗?或者你点开他那里计算某一天是星期几的公式放上来看看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-7 00:01:16 | 显示全部楼层
数字转英文:
  1. # 将 0 ~ 9 的数字转换成英文
  2. def unit_to_word(u):
  3.     words = {
  4.         0: 'zero', 1: 'one', 2: 'two', 3: 'three',
  5.         4: 'four', 5: 'five', 6: 'six', 7: 'seven',
  6.         8: 'eight', 9: 'nine'
  7.     }
  8.     return words[u]

  9. # 将 10 ~ 19,20 ~ 99 的十位部分数字转换成英文
  10. def tens_to_word(t):
  11.     words = {
  12.         10: 'ten', 11: 'eleven', 12: 'twelve',
  13.         13: 'thirteen', 14: 'fourteen', 15: 'fifteen',
  14.         16: 'sixteen', 17: 'seventeen', 18: 'eighteen',
  15.         19: 'nineteen', 20: 'twenty', 30: 'thirty',
  16.         40: 'forty', 50: 'fifty', 60: 'sixty',
  17.         70: 'seventy', 80: 'eighty', 90: 'ninety'
  18.     }
  19.     if 10 <= t < 20 or not t % 10:
  20.         return words[t]
  21.     else:
  22.         a, b = divmod(t, 10)
  23.         return words[a*10] + ' ' + unit_to_word(b)

  24. # 将 百位数字转换成英文
  25. def hundreds_to_word(h):
  26.     a, b = divmod(h, 100)
  27.     if b < 10:
  28.         return f"{unit_to_word(a)} hundred and {unit_to_word(b)}"
  29.     else:
  30.         return f"{unit_to_word(a)} hundred and {tens_to_word(b)}"

  31. print(hundreds_to_word(729))
复制代码


输出日历:
  1. def day(y, m, d):
  2.     ans = 0
  3.     month = [31,29,31,30,31,30,31,31,30,31,30,31]

  4.     if not isLeapYear(y):
  5.         month[1] = 28

  6.     for i in range(1,y):  # 注意计算之前年的天数时要从1开始计算
  7.         if isLeapYear(i):
  8.             ans += 366
  9.             # ans = ans % 7
  10.         else:
  11.             ans += 365
  12.             # ans = ans % 7

  13.     for i in range(m-1):   # 这里因为月份是由数组表示的,从1月加到m-1月在数组中就是从下标0加到下标m-2
  14.         ans +=  month[i]
  15.         # ans = ans % 7

  16.     ans += d

  17.     return ans % 7# 最后这里对7取余也可以,如果计算的年数过多可以边加天数,边对7取余,就像注释掉的那些代码一样

  18. def isLeapYear(y):
  19.     if (y % 400 == 0) or (y % 100 != 0 and y % 4 == 0):
  20.         return True
  21.     else:
  22.         return False

  23. def calendar(y, m):
  24.     month = [31,29,31,30,31,30,31,31,30,31,30,31]

  25.     if not isLeapYear(y):
  26.         month[1] = 28
  27.     print(f"\t\t\t{y}年{m}月\t\t\t")
  28.     print()
  29.     print('\t'.join(['Su', 'M', 'Tu', 'W', 'Th', 'F', 'Sa']))
  30.     temp = [''] * 7
  31.     d = month[m-1]
  32.     for i in range(1, d + 1):
  33.         temp[day(y, m, i)] = str(i)
  34.         if temp[-1] != '' or i == d:
  35.             print('\t'.join(temp))
  36.             temp = [''] * 7

  37. calendar(2022, 3)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-7 07:58:15 | 显示全部楼层

Screenshot_20220307_075637.jpg
那个转化成英文的输出形式不对?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-7 08:03:38 | 显示全部楼层
风百默 发表于 2022-3-7 07:58
那个转化成英文的输出形式不对?

个位数的跟十位数的是另两个函数,你看一下注释,我有写着注释的,

题目不是要求 转换个位,十位,百位的用不同函数吗
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-7 08:10:50 | 显示全部楼层
风百默 发表于 2022-3-7 07:58
那个转化成英文的输出形式不对?

我改一下,可能是我理解错了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-7 08:18:35 | 显示全部楼层
本帖最后由 isdkz 于 2022-3-8 20:39 编辑
风百默 发表于 2022-3-7 07:58
那个转化成英文的输出形式不对?


如果都要用 hundreds_to_word 这个函数转换的话,就改成这样
  1. # 将 0 ~ 9 的数字转换成英文
  2. def unit_to_word(u):
  3.     words = {
  4.         0: 'zero', 1: 'one', 2: 'two', 3: 'three',
  5.         4: 'four', 5: 'five', 6: 'six', 7: 'seven',
  6.         8: 'eight', 9: 'nine'
  7.     }
  8.     return words[u]

  9. # 将 10 ~ 19,20 ~ 99 的十位部分数字转换成英文
  10. def tens_to_word(t):
  11.     words = {
  12.         10: 'ten', 11: 'eleven', 12: 'twelve',
  13.         13: 'thirteen', 14: 'fourteen', 15: 'fifteen',
  14.         16: 'sixteen', 17: 'seventeen', 18: 'eighteen',
  15.         19: 'nineteen', 20: 'twenty', 30: 'thirty',
  16.         40: 'forty', 50: 'fifty', 60: 'sixty',
  17.         70: 'seventy', 80: 'eighty', 90: 'ninety'
  18.     }
  19.     if 10 <= t < 20 or not t % 10:
  20.         return words[t]
  21.     else:
  22.         a, b = divmod(t, 10)
  23.         return words[a*10] + ' ' + unit_to_word(b)

  24. # 将 百位数字转换成英文
  25. def hundreds_to_word(h):
  26.     if h <10:
  27.         return unit_to_word(h)
  28.     elif 10 <= h < 100:
  29.         return tens_to_word(h)
  30.     elif h % 100 == 0:
  31.         return f"{unit_to_word(h//100)} hundred"
  32.     else:
  33.         a, b = divmod(h, 100)
  34.         if b < 10:
  35.             return f"{unit_to_word(a)} hundred and {unit_to_word(b)}"
  36.         else:
  37.             return f"{unit_to_word(a)} hundred and {tens_to_word(b)}"

  38. print(hundreds_to_word(99))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-8 20:31:33 | 显示全部楼层
isdkz 发表于 2022-3-7 08:18
如果都要用 hundreds_to_word 这个函数转换的话,就改成这样


还是有点问题
-45c1d189cb7807f0.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-8 20:35:02 | 显示全部楼层
isdkz 发表于 2022-3-7 08:18
如果都要用 hundreds_to_word 这个函数转换的话,就改成这样

大佬,return f"{}and{}"是什么形式的返回方式
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-8 20:41:21 | 显示全部楼层
风百默 发表于 2022-3-8 20:35
大佬,return f"{}and{}"是什么形式的返回方式

f-string的字符串格式化方式,python 格式化字符串的方式你可以看一下这个:

https://mp.weixin.qq.com/s/mpjDJ50T5ZjskKIIZO6_vQ
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-8 20:41:59 | 显示全部楼层
风百默 发表于 2022-3-8 20:35
大佬,return f"{}and{}"是什么形式的返回方式

你试一下这个:
  1. # 将 0 ~ 9 的数字转换成英文
  2. def unit_to_word(u):
  3.     words = {
  4.         0: 'zero', 1: 'one', 2: 'two', 3: 'three',
  5.         4: 'four', 5: 'five', 6: 'six', 7: 'seven',
  6.         8: 'eight', 9: 'nine'
  7.     }
  8.     return words[u]

  9. # 将 10 ~ 19,20 ~ 99 的十位部分数字转换成英文
  10. def tens_to_word(t):
  11.     words = {
  12.         10: 'ten', 11: 'eleven', 12: 'twelve',
  13.         13: 'thirteen', 14: 'fourteen', 15: 'fifteen',
  14.         16: 'sixteen', 17: 'seventeen', 18: 'eighteen',
  15.         19: 'nineteen', 20: 'twenty', 30: 'thirty',
  16.         40: 'forty', 50: 'fifty', 60: 'sixty',
  17.         70: 'seventy', 80: 'eighty', 90: 'ninety'
  18.     }
  19.     if 10 <= t < 20 or not t % 10:
  20.         return words[t]
  21.     else:
  22.         a, b = divmod(t, 10)
  23.         return words[a*10] + ' ' + unit_to_word(b)

  24. # 将 百位数字转换成英文
  25. def hundreds_to_word(h):
  26.     if h <10:
  27.         return unit_to_word(h)
  28.     elif 10 <= h < 100:
  29.         return tens_to_word(h)
  30.     elif h % 100 == 0:
  31.         return f"{unit_to_word(h//100)} hundred"
  32.     else:
  33.         a, b = divmod(h, 100)
  34.         if b < 10:
  35.             return f"{unit_to_word(a)} hundred and {unit_to_word(b)}"
  36.         else:
  37.             return f"{unit_to_word(a)} hundred and {tens_to_word(b)}"

  38. print(hundreds_to_word(99))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-8 22:05:40 | 显示全部楼层

大佬,这个第八行和16行为什么要用个循环,这是什么意思
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-8 22:09:17 | 显示全部楼层
风百默 发表于 2022-3-8 22:05
大佬,这个第八行和16行为什么要用个循环,这是什么意思

为了计算特定日期到公元 1 年 1 月 1日距离多少天
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-30 00:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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