Run ID 作者 问题 语言 测评结果 时间 内存 代码长度 提交时间
85152 sh25_wangtaojie 递归求阶乘 Python3 通过 28 MS 3716 KB 845 2026-03-06 15:15:34

Tests(3/3):


def factorial_iterative(n): """ 使用迭代方法计算阶乘 """ if n < 0: raise ValueError("阶乘不能为负数") result = 1 for i in range(1, n + 1): result *= i return result def factorial_recursive(n): """ 使用递归方法计算阶乘 """ if n < 0: raise ValueError("阶乘不能为负数") if n == 0 or n == 1: return 1 else: return n * factorial_recursive(n - 1) def main(): try: n = int(input()) # 使用迭代方法计算阶乘(效率更高) result = factorial_iterative(n) print(result) except ValueError as e: print(f"输入错误: {e}") except Exception as e: print(f"计算出错: {e}") if __name__ == "__main__": main()


测评信息: