编辑代码

# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
#print("Hello world!   -  python.jsrun.net .")
def long_root_extraction(x, y):
    # 处理输入并分组
    s = str(x)
    # 补前导零使长度为y的倍数
    if len(s) % y != 0:
        s = '0' * (y - (len(s) % y)) + s
    groups = [s[i:i+y] for i in range(0, len(s), y)]
    
    root = 0
    remainder = 0
    steps = []
    
    for i, group in enumerate(groups):
        current_group = int(group)
        current_value = remainder * (10 ** y) + current_group
        
        # 寻找最大的d
        max_d = 0
        term = 0
        for d in range(9, -1, -1):
            # 计算差值:(root*10 + d)^y - (root*10)^y
            term_candidate = (root * 10 + d) ** y - (root * 10) ** y
            if term_candidate <= current_value:
                max_d = d
                term = term_candidate
                break
        
        # 记录步骤信息
        steps.append({
            'step': i + 1,
            'group': group,
            'current_value': current_value,
            'd': max_d,
            'term': term,
            'remainder': current_value - term,
            'current_root': root * 10 + max_d
        })
        
        # 更新根和余数
        remainder = current_value - term
        root = root * 10 + max_d
    
    return root, steps

def print_steps(steps, y):
    for step in steps:
        print(f"步骤 {step['step']}:")
        print(f"处理分组:{step['group']}")
        print(f"当前值:{step['current_value']}")
        print(f"尝试找到最大的d,使得 ({step['current_root'] // 10}×10 + d)^{y} - ({step['current_root'] // 10}×10)^{y}{step['current_value']}")
        print(f"找到d = {step['d']},因为 {step['term']}{step['current_value']}")
        print(f"余数:{step['current_value']} - {step['term']} = {step['remainder']}")
        print(f"当前根:{step['current_root']}\n")
        print("-" * 50)

# 示例用法
x = int(input('需根数字:'))
y = int(input('需根次数:'))
root, steps = long_root_extraction(x, y)
print(f"最终结果:{x}{y}次方根整数部分为{root}")
print("\n详细步骤:")
print_steps(steps, y)