|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.输入两个正整数A和B,输出A/B化为小数后的循环节- # 2022.7.18小练习1
- '''
- cache用于存储字符串,len(cache)有多长就往后检查多长看是否有无重复
- 若重复则则为循环节,但有缺陷,比如114114115这种循环节就不行
- 好吧甚至连114这种都不行
- 暂时没想到有什么更好的方法解决这个问题
- '''
- def xunHuanJie(str1):
- '''用于查找循环节的函数,接受str类型的实参'''
- cache = ''
- # cache用于存放临时检查的字符串(这个说法不太好,但不知道换那个说法更好些)
- start = str1.index('.')
- # 从小数点后开始检查
- for x in range(start + 1, len(str1)):
- cache += str1[x]
- if str1[x + 1: x + len(cache) + 1] == cache:
- # 检查后len(cache)位,看是否与cache相等
- return cache
- a, b = map(int, input('a/b?').split('/'))
- str1 = str(a/b)
- xhj = xunHuanJie(str1)
- print(xhj)
复制代码
顺便问下有什么修改用户名的方法吗,好久以前取得现在想扇那时候的自己一巴掌
本帖最后由 qq1151985918 于 2022-7-25 22:31 编辑
- def get_repetend(A, B):
- list_quotient = []
- list_remainder = []
- int_quotient = A // B
- while True:
- A = A % B
- if A in list_quotient:
- index = list_quotient.index(A)
- list_repetend = list_remainder[index:]
- list_before_repetend = list_remainder[:index]
- break
- list_remainder.append(A * 10 // B)
- list_quotient.append(A % B)
- A = A * 10
- repetend = ''.join(map(str, list_repetend))
- before_repetend = ''.join(map(str, list_before_repetend))
- result = f'{int_quotient}.{before_repetend}({repetend})'
- return result
- if __name__ == '__main__':
- print(get_repetend(5, 12))
- print(get_repetend(3, 17))
复制代码
|
|