题目描述:
编写程序实现将任意十进制正小数m转换成n进制的正小数,小数点后保留10位小数。
解答要求:
时间限制1000ms,内存限制64MB。
输入:
输入包含两个数m、n,用空格隔开。输入包含多组测试,当m、n都为0时输入结束。
Limits:
1、0.0000009<m<1
2、1<n<10
输出:
输出10进制正小数m的n进制小数,结果保留10位小数。
样例:
输入样例:0.795 3
输出样例:0.2101101122
#导入decimal模块中的Decimal和getcontext,decimal意思为十进制,这个模块提供了十进制浮点运算支持
from decimal import Decimal,getcontext
def func():
#无限循环输入两个数并赋值给m,n
while True:
try:
m,n=input().strip().split()
m=Decimal(m)
n=int(n)
except EOFError:
break
#有界精度:用于存储数字的位数是固定的,可以通过decimal.getcontext().prec=x 来设定
getcontext().prec=10
string_m=Decimal(m)
result=[]
count=0
if string_m==0 and n==0:
break
else:
while string_m>0 and count<10:
string_m=(string_m*n)
result.append(str(string_m)[0])