找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 6744|回复: 4

[代码与实例] 20以内加减法生成

1

主题

9

帖子

9

积分

贫民

积分
9
portkiller 发表于 2017-2-27 21:14:05 | 显示全部楼层 |阅读模式
今天孩子老师让出20以内加减法20道,凭脑子想难免不重复,写了一段代码自动生成
  1. # coding:utf-8
  2. import random

  3. ListTi = []

  4. i = 1

  5. while len(ListTi) <= 50:
  6.     Amath = random.randint(1, 20)
  7.     Bmath = random.randint(1, 20)

  8.     if Amath > Bmath and Bmath < 10:
  9.         jianfa1 = "%d - %d" % (Amath, Bmath)
  10.         while jianfa1 not in ListTi:
  11.             ListTi.append(jianfa1)
  12.             i += i
  13.     elif Amath < Bmath and Amath < 10:
  14.         jianfa2 = "%d - %d" % (Bmath, Amath)
  15.         if jianfa2 not in ListTi:
  16.             ListTi.append(jianfa2)
  17.             i += i
  18. while len(ListTi) <= 100:
  19.     Amath = random.randint(1, 20)
  20.     Bmath = random.randint(1, 20)

  21.     if Amath + Bmath < 20:
  22.         jiafa = "%d + %d" % (Amath, Bmath)
  23.         if jiafa not in ListTi:
  24.             ListTi.append(jiafa)
  25.             i += i

  26. for ii in range(1, len(ListTi)):
  27.     print "第 %d 道" % ii
  28.     print ListTi[ii]

复制代码


回复

使用道具 举报

0

主题

33

帖子

33

积分

贫民

积分
33
jackyspy 发表于 2017-3-2 16:01:49 | 显示全部楼层
本帖最后由 jackyspy 于 2017-3-7 11:56 编辑

该方案存在严重概率错误,请参看后面的解决方案

和在2~19之间,1<=加数<和,以此构造。
  1. # coding:utf-8
  2. from __future__ import print_function
  3. from random import randint


  4. def iter_equations(count):
  5.     uniq = set()
  6.     for _ in range(count):
  7.         sum_ = randint(2, 19)
  8.         a = randint(1, sum_ - 1)
  9.         if (sum_, a) not in uniq:
  10.             uniq.add((sum_, a))
  11.             yield sum_, a


  12. for sum_, a in iter_equations(50):
  13.     print(a, '+', sum_ - a)

  14. for sum_, a in iter_equations(50):
  15.     print(sum_, '-', a)
复制代码
回复 支持 反对

使用道具 举报

0

主题

1

帖子

1

积分

贫民

积分
1
mysaber 发表于 2017-3-2 16:19:57 | 显示全部楼层
哈哈哈,警察叔叔就是这个坏人出的题目
回复 支持 反对

使用道具 举报

0

主题

33

帖子

33

积分

贫民

积分
33
jackyspy 发表于 2017-3-7 11:51:27 | 显示全部楼层
本帖最后由 jackyspy 于 2017-3-7 12:02 编辑

转换为从1~19中选2个数的组合,共计19*18/2组。

  1. import random
  2. import itertools


  3. def get_combination(n, m):
  4.     return random.sample(list(itertools.combinations(range(1, n + 1), 2)), m)


  5. for a, sum_ in get_combination(19, 50):
  6.     print(a, '+', sum_ - a)

  7. for a, sum_ in get_combination(19, 50):
  8.     print(sum_, '-', a)
复制代码

上面的方案因为构造了所有组合,在数量较小的情况下效率很高。但是当数字范围增加到一定程度时,再构造所有组合就存在很大开销。比如构造1000个10000以内的加减法。对于这种情况,只要从1~9999中随机抽取两个不同的数,大的作和就行了。

  1. import random


  2. def get_combination(n, m):
  3.     result = []
  4.     for i in random.sample(range(n * (n - 1)), m):
  5.         x, y = divmod(i, n)
  6.         result.append((x + 1, y + 1) if x < y else (y + 1, x + 2))

  7.     return result


  8. for a, sum_ in get_combination(19, 50):
  9.     print(a, '+', sum_ - a)

  10. for a, sum_ in get_combination(19, 50):
  11.     print(sum_, '-', a)
复制代码



回复 支持 反对

使用道具 举报

0

主题

27

帖子

27

积分

贫民

积分
27
艾幻翔 发表于 2017-8-22 16:01:14 | 显示全部楼层
本帖最后由 艾幻翔 于 2017-8-22 16:16 编辑

不重复的题目,且答案不为负数. 用生成器而不要用列表
  1. #!/usr/bin/env python
  2. # -*- coding: gbk -*-
  3. # Created by lightwave on 2017/8/17
  4. import random


  5. def get_combination(min=1, max=20):
  6.     history = []
  7.     max_count = ((max - min + 1) ** 2) * 3 / 2
  8.     while True:
  9.         if len(history) >= max_count:
  10.             raise StopIteration()

  11.         number1 = random.randint(min, max)
  12.         operator_flag = random.randint(0, 1)
  13.         _max = max if operator_flag else number1
  14.         number2 = random.randint(min, _max)

  15.         combination = (number1, operator_flag, number2)
  16.         if combination not in history:
  17.             history.append(combination)
  18.             yield combination


  19. if __name__ == '__main__':
  20.     combination = get_combination()

  21.     for number1, operator_flag, number2 in combination:
  22.         operator = '+' if operator_flag else '-'
  23.         print(number1, operator, number2, '= ')
复制代码


回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表