生成数学题目

有余数的除法竖式计算

import random

def generate_vertical_division_problems(num_problems):
    problems_with_answers = []
    while len(problems_with_answers) < num_problems:
        # Generate random numbers for the problem
        dividend = random.randint(10, 99)  # random two-digit number
        divisor = random.randint(2, 9)  # random single-digit number

        # Calculate quotient and remainder
        quotient = dividend // divisor
        remainder = dividend % divisor

        # Check if remainder is zero; if so, skip this iteration
        if remainder == 0:
            continue

        # Format the problem
        problem = f" {len(problems_with_answers) + 1}: {dividend} ÷ {divisor} ="

        # Store problem and answer separately
        problems_with_answers.append((problem, (quotient, remainder)))

    return problems_with_answers

def save_problems_and_answers(problems_with_answers, file_name, overwrite=False):
    mode = 'w' if overwrite else 'a'
    with open(file_name, mode) as f:
        for problem, (quotient, remainder) in problems_with_answers:
            f.write(f"{problem}\n")

        if overwrite:
            f.write("\nAnswers:\n\n")

        for index, (_, (quotient, remainder)) in enumerate(problems_with_answers, start=1):
            answer = f" {index}: {quotient}......{remainder}\n"
            f.write(answer)

# Generate 1000 vertical division problems
num_problems = 1000
problems_with_answers = generate_vertical_division_problems(num_problems)

# Save problems and answers to a single file, overwriting any existing file
file_name = 'division_problems_and_answers.txt'
save_problems_and_answers(problems_with_answers, file_name, overwrite=True)

print(f"Division problems and answers saved to '{file_name}'.")

选三位数进位加退位减各10道列竖式计算

import random


def generate_addition_problems(num_problems):
    problems_with_answers = []
    while len(problems_with_answers) < num_problems:
        # Generate random numbers for the problem
        num1 = random.randint(100, 999)  # random three-digit number
        num2 = random.randint(10, 99)  # random two-digit number

        # Calculate the problem and answer
        problem = f"{num1}\n+ {num2}\n---"
        answer = num1 + num2

        # Store problem and answer together
        problem_with_answer = (problem, answer)

        # Append to list of problems with answers
        problems_with_answers.append(problem_with_answer)

    return problems_with_answers


def generate_subtraction_problems(num_problems):
    problems_with_answers = []
    while len(problems_with_answers) < num_problems:
        # Generate random numbers for the problem
        num1 = random.randint(100, 999)  # random three-digit number
        num2 = random.randint(10, 99)  # random two-digit number

        # Ensure num1 is greater than num2 for valid subtraction
        if num1 < num2:
            num1, num2 = num2, num1

        # Calculate the problem and answer
        problem = f"{num1}\n- {num2}\n---"
        answer = num1 - num2

        # Store problem and answer together
        problem_with_answer = (problem, answer)

        # Append to list of problems with answers
        problems_with_answers.append(problem_with_answer)

    return problems_with_answers


def save_problems_and_answers(problems_with_answers, file_name, overwrite=False):
    problems_only = []
    answers_only = []
    with open(file_name, 'w') as f:
        for index, (problem, answer) in enumerate(problems_with_answers, start=1):
            # Store problem in list
            problems_only.append(f"题目 {index}:\n{problem}\n\n\n\n")
            # Store answer in list
            answers_only.append(f"Answer {index}: {answer}\n")

        # Write all problems to file
        f.write("Problems:\n\n")
        for problem in problems_only:
            f.write(problem)
        import random

        def generate_addition_problems(num_problems):
            problems_with_answers = []
            for _ in range(num_problems):
                # Generate random numbers for the problem
                num1 = random.randint(100, 999)  # random three-digit number
                num2 = random.randint(10, 99)  # random two-digit number

                # Calculate the problem and answer
                problem = f"{num1}+ {num2}=\n---"
                answer = num1 + num2

                # Store problem and answer together
                problem_with_answer = (problem, answer)

                # Append to list of problems with answers
                problems_with_answers.append(problem_with_answer)

            return problems_with_answers

        def generate_subtraction_problems(num_problems):
            problems_with_answers = []
            for _ in range(num_problems):
                # Generate random numbers for the problem
                num1 = random.randint(100, 999)  # random three-digit number
                num2 = random.randint(10, 99)  # random two-digit number

                # Ensure num1 is greater than num2 for valid subtraction
                if num1 < num2:
                    num1, num2 = num2, num1

                # Calculate the problem and answer
                problem = f"{num1}- {num2}= \n---"
                answer = num1 - num2

                # Store problem and answer together
                problem_with_answer = (problem, answer)

                # Append to list of problems with answers
                problems_with_answers.append(problem_with_answer)

            return problems_with_answers

        def save_problems_and_answers(problems_with_answers, file_name, overwrite=False):
            problems_only = []
            answers_only = []
            with open(file_name, 'w') as f:
                for index, (problem, answer) in enumerate(problems_with_answers, start=1):
                    # Store problem in list
                    problems_only.append(f"题目 {index}:\n{problem}\n\n")
                    # Store answer in list
                    answers_only.append(f"Answer {index}: {answer}\n")

                # Write all problems to file
                f.write("Problems:\n\n")
                for problem in problems_only:
                    f.write(problem)

                # Write all answers to file
                f.write("\n\nAnswers:\n\n")
                for answer in answers_only:
                    f.write(answer)

        # Generate 10 addition problems and 10 subtraction problems as one group
        num_problems_per_type = 1000
        addition_problems = generate_addition_problems(num_problems_per_type)
        subtraction_problems = generate_subtraction_problems(num_problems_per_type)

        # Save problems and answers to a single file, overwriting any existing file
        file_name = '选三位数进位加退位减各10道列竖式计算.txt'
        save_problems_and_answers(addition_problems + subtraction_problems, file_name, overwrite=True)

        print(f"Addition and subtraction problems saved to '{file_name}'.")
        # Write all answers to file
        f.write("\n\nAnswers:\n\n")
        for answer in answers_only:
            f.write(answer)


# Generate 10 addition problems and 10 subtraction problems
num_problems = 1000
addition_problems = generate_addition_problems(num_problems)
subtraction_problems = generate_subtraction_problems(num_problems)

# Save problems and answers to a single file, overwriting any existing file
file_name = '选三位数进位加退位减各10道列竖式计算.txt'
save_problems_and_answers(addition_problems + subtraction_problems, file_name, overwrite=True)

print(f"Addition and subtraction problems saved to '{file_name}'.")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值