• python练习题(二)


    题目:

     1 已知以下几期双色球号码(最后一个数字为蓝球),
     2 2019080 03 06 08 20 24 32  07
     3 2019079 01 03 06 09 19 31  16
     4 2019078 01 17 27 29 31 33  12
     5 2019077 09 11 13 18 21 22  15
     6 2019076 12 15 19 20 29 32  14
     7 2019075 05 08 20 22 31 33  03
     8 2019074 12 21 27 29 31 33  04
     9 2019073 01 02 06 12 16 18  08
    10 2019072 02 09 13 15 22 30  15
    11 2019071 01 08 19 24 29 30  04
    12  
    13 要求:
    14 一、运行后展示:
    15 1.查询双色球号码:
    16 2.查询出现号码次数
    17 3.输入新号码
    18 q.退出
    19  
    20 如果输入1:
    21 1.2019080
    22 2.2019079
    23 3.·····(效果同上)
    24 q.退出
    25 输入号码后展示对应的结果;如果号码不存在或者非数字提示请重新输入
    26 如果输入2
    27 结果展示 红球中各个号码出现的次数、蓝球中各个号码出现的次数;
    28 如果输入3(附加题,选做)
    29 新增的号码支持前面功能 1、2;退出后新增的号码不会保存;如果输入非数字或红球大于33 蓝球大于16 提示请重新输入
    30 如果输入q 退出

    小余的答案:

      1 #!/opt/python27/bin/python
      2 # -*- coding:utf-8 -*-
      3 # author:why
      4 # date:20190715
      5 
      6 '''
      7 已知以下几期双色球号码(最后一个数字为蓝球),
      8 2019080 03 06 08 20 24 32  07
      9 2019079 01 03 06 09 19 31  16
     10 2019078 01 17 27 29 31 33  12
     11 2019077 09 11 13 18 21 22  15
     12 2019076 12 15 19 20 29 32  14
     13 2019075 05 08 20 22 31 33  03
     14 2019074 12 21 27 29 31 33  04
     15 2019073 01 02 06 12 16 18  08
     16 2019072 02 09 13 15 22 30  15
     17 2019071 01 08 19 24 29 30  04
     18  
     19 要求:
     20 一、运行后展示:
     21 1.查询双色球号码:
     22 2.查询出现号码次数
     23 3.输入新号码(暂不实现)
     24 q.退出
     25  
     26 如果输入1:
     27 1.2019080
     28 2.2019079
     29 3.·····(效果同上)
     30 q.退出
     31 输入号码后展示对应的结果;如果号码不存在或者非数字提示请重新输入
     32  
     33 如果输入2
     34 结果展示 红球中各个号码出现的次数、蓝球中各个号码出现的次数;
     35  
     36 如果输入3(附加题,选做)
     37 新增的号码支持前面功能 1、2;退出后新增的号码不会保存;如果输入非数字或红球大于33 蓝球大于16 提示请重新输入
     38  
     39 如果输入q 退出
     40 '''
     41 
     42 
     43 class doubleSle:
     44     def __init__(self):
     45         douData = {2019080: [03, 06, 8, 20, 24, 32, 07],
     46                    2019079: [01, 03, 06, 9, 19, 31, 16],
     47                    2019078: [01, 17, 27, 29, 31, 33, 12],
     48                    2019077: [9, 11, 13, 18, 21, 22, 15],
     49                    2019076: [12, 15, 19, 20, 29, 32, 14],
     50                    2019075: [05, 8, 20, 22, 31, 33, 03],
     51                    2019074: [12, 21, 27, 29, 31, 33, 04],
     52                    2019073: [01, 02, 06, 12, 16, 18, 8],
     53                    2019072: [02, 9, 13, 15, 22, 30, 15],
     54                    2019071: [01, 8, 19, 24, 29, 30, 04]}
     55         self.douData = douData
     56         seleOne = ['1.查询双色球号码:', '2.查询出现号码次数:', 'q.退出']
     57         seleTwo = sorted(self.douData.keys())
     58         self.seleOne = seleOne
     59         self.seleTwo = seleTwo
     60 
     61     def doubleSleCount(self):
     62         '''
     63         统计红球、篮球出现次数
     64         :return: 
     65         '''
     66         countRed=[0]
     67         countBlue = [0]
     68         for i in range(1,34):
     69             countRed.append(0)
     70             for j in range(0,len(self.douData.values()[0])-1):
     71                 for k in range(0,len(self.douData.keys())):
     72                     if i == self.douData.values()[k][j]:
     73                         countRed[i] +=1
     74 
     75         for i in range(1,17):
     76             countBlue.append(0)
     77             for j in range(0, len(self.douData.keys() )):
     78                 if i == self.douData.values()[j][len(self.douData.values()[0])-1]:
     79                     countBlue[i] +=1
     80 
     81         print countRed,countBlue
     82         return [countRed,countBlue]
     83 
     84     def doubleSleRaw(self,liRes,num):
     85         '''
     86         将列表转成str,并换行
     87         :param liRes: 
     88         :return: 
     89         '''
     90         s = ''
     91         if num == 0:
     92             for i in liRes:
     93                 s += i+'	
    '
     94         elif num == 1:
     95             for i in range(0,len(liRes)):
     96                 s += '{a}.{b}	
    '.format(a=(i+1),b=liRes[i])
     97         return s
     98 
     99     def begin(self):
    100         '''
    101         z循环开始
    102         :return: 
    103         '''
    104         while True:
    105             # doubleSleC = doubleSle()
    106             numOne = raw_input(self.doubleSleRaw(self.seleOne,0))
    107             # 判断是否是整数
    108             if numOne.isdigit() :
    109                 numOneInt = int(numOne)
    110                 if numOneInt == 1:
    111                     numTwo = raw_input(self.doubleSleRaw(self.seleTwo,1))
    112                     if numTwo.isdigit():
    113                         numTwoInt = int(numTwo)
    114                         if numTwoInt >0 and numTwoInt <= len(sorted(self.douData.keys())):
    115                             print('查询结果:{a} {b}'.format(a=(sorted(self.douData.keys()))[numTwoInt-1],b=self.douData[(sorted(self.douData.keys()))[numTwoInt-1]]))
    116                         else:
    117                             print('请重新输入')
    118                             continue
    119                     else:
    120                         print('请重新输入')
    121                         continue
    122                 elif numOneInt == 2:
    123                     doubleSleCount = self.doubleSleCount()
    124                     print('红球出现的次数:')
    125                     for i in range(0,len(doubleSleCount[0])-1):
    126                         print('{a}出现的次数:{b}').format(a=(i+1),b=doubleSleCount[0][i+1])
    127                     print('篮球出现的次数:')
    128                     for i in range(0, len(doubleSleCount[1])-1):
    129                         print('{a}出现的次数:{b}').format(a=(i+1),b=doubleSleCount[1][i+1])
    130                 else:
    131                     print('请重新输入')
    132                     continue
    133             else:
    134                 if numOne == 'q':
    135                     exit()
    136                 else:
    137                     print('请输入数字')
    138 
    139 if __name__ == '__main__':
    140     s = doubleSle()
    141     s.begin()
    View Code

    敏敏的答案:

      1 #!/usr/bin/env python
      2 # -*- coding: utf-8 -*-
      3 # @File  : DoubleColorBall.py
      4 # @Author: xuemin.feng
      5 # @Date  : 2019/7/15
      6 
      7 """
      8 已知以下几期双色球号码(最后一个数字为蓝球),
      9 2019080 03 06 08 20 24 32  07
     10 2019079 01 03 06 09 19 31  16
     11 2019078 01 17 27 29 31 33  12
     12 2019077 09 11 13 18 21 22  15
     13 2019076 12 15 19 20 29 32  14
     14 2019075 05 08 20 22 31 33  03
     15 2019074 12 21 27 29 31 33  04
     16 2019073 01 02 06 12 16 18  08
     17 2019072 02 09 13 15 22 30  15
     18 2019071 01 08 19 24 29 30  04
     19 
     20 要求:
     21 一、运行后展示:
     22 1.查询双色球号码:
     23 2.查询出现号码次数
     24 3.输入新号码
     25 q.退出
     26 
     27 如果输入1:
     28 1.2019080
     29 2.2019079
     30 3.·····(效果同上)
     31 q.退出
     32 输入号码后展示对应的结果;如果号码不存在或者非数字提示请重新输入
     33 
     34 如果输入2
     35 结果展示 红球中各个号码出现的次数、蓝球中各个号码出现的次数;
     36 
     37 如果输入3(附加题,选做)
     38 新增的号码支持前面功能 1、2;退出后新增的号码不会保存;如果输入非数字或红球大于33 蓝球大于16 提示请重新输入
     39 如果输入q 退出
     40 
     41 思路:
     42 1、拆成dict{"2019071":"01 08 19 24 29 30  04",,,,,,,},根据key取value
     43 2、红球、篮球分别放list里,遍历记录次数
     44 """
     45 
     46 
     47 # 将原始字符串变换为list,再转换成dict,方便按期数取值
     48 def balls2dict():
     49     s1 = "2019080 03 06 08 20 24 32  07"
     50     s2 = "2019079 01 03 06 09 19 31  16"
     51     s3 = "2019078 01 17 27 29 31 33  12"
     52     s4 = "2019077 09 11 13 18 21 22  15"
     53     s5 = "2019076 12 15 19 20 29 32  14"
     54     s6 = "2019075 05 08 20 22 31 33  03"
     55     s7 = "2019074 12 21 27 29 31 33  04"
     56     s8 = "2019073 01 02 06 12 16 18  08"
     57     s9 = "2019072 02 09 13 15 22 30  15"
     58     s10 = "2019071 01 08 19 24 29 30  04"
     59     # 先转换成list
     60     balllist = [s1, s2, s3, s4, s5, s6, s7, s8, s9, s10]
     61 
     62     # 根据题意,转换成dict,方便根据期数取值
     63     balldict = []
     64     for i in range (len (balllist)):
     65         dic = {balllist[i][:7]: balllist[i][8:]}
     66         balldict.append (dic)
     67 
     68     return balldict
     69 
     70 # 取出key生成列表,按期数倒序
     71 def getKeys():
     72     balldict = balls2dict ()
     73     keys = []
     74     for i in range (len (balldict)):
     75         keys.append (balldict[i].keys ())
     76         keys.sort (reverse=True)
     77     return keys
     78 
     79 # 根据输入数字,输出期数
     80 def fromNum2Key():
     81     num_date = []
     82     keys = getKeys ()
     83     for i in range (len (keys)):
     84         num_date.append (str (i + 1) + "." + str (keys[i]).replace ("['", "").replace ("']", ""))
     85         print num_date[i]
     86 
     87 # 将红篮球拆分,分别计算重复次数
     88 def getValues():
     89     balldict = balls2dict ()
     90     values = []
     91     redballs = []
     92     blueballs = []
     93 
     94     # 将每组红蓝球放一个集合
     95     for i in range (len (balldict)):
     96         values.append (str (balldict[i].values ()).replace ("['", "").replace ("']", "").split (" "))
     97     # return values
     98 
     99     # 将所有红球放一个大集合,准备计算次数
    100     for i in range (10):
    101         for j in range (6):
    102             redballs.append (values[i][j])
    103     print redballs
    104 
    105     # 红球出现的次数(网上查的set去重方法,简单的一笔,sorted排序)
    106     redballs_set = sorted (set (redballs))
    107 
    108     for redball in redballs_set:
    109         print ("红球 %s 出现过 %s 次" % (redball, redballs.count (redball)))
    110 
    111     # 篮球出现的次数
    112     for i in range (10):
    113         for j in range (7, 8):
    114             blueballs.append (values[i][j])
    115     # print blueballs
    116     blueballs_set = sorted (set (blueballs))
    117     for blueball in blueballs_set:
    118         print ("蓝球 %s 出现过 %s 次" % (blueball, blueballs.count (blueball)))
    119 
    120 def entranceEx():
    121     while True:
    122         one = raw_input ("1.查询双色球号码:" + "
    " + "2.查询出现号码次数" + "
    " + "3.输入新号码" + "
    " + "q.退出" + "
    " + "输入以上序号进入:")
    123         if one == 'q':
    124             exit ()
    125         elif int (one) == 1:
    126             fromNum2Key ()
    127             two = input ("#输入序号查看开奖结果:")
    128             balldict = balls2dict ()
    129             two_value = str (balldict[two - 1].values ()).replace ("['", "").replace ("']", "")
    130             print two_value + "
    " + "---------------------" + "
    "
    131         elif int (one) == 2:
    132             # print "TODO-查询出现号码次数"
    133             getValues ()
    134             print "---------------------" + "
    "
    135 
    136         elif int (one) == 3:
    137             print "TODO-输入新号码"
    138             print "---------------------" + "
    "
    139 
    140         else:
    141             print "#输入有误"
    142 
    143 if __name__ == '__main__':
    144     # print balls2dict()
    145     # print fromNum2Key()
    146     print getValues()
    147     # print entranceEx ()
    View Code

    四爷的答案:

     1 # encoding: utf-8
     2 
     3 
     4 
     5 doublelist = {2019080: [03, 06, 8, 20, 24, 32, 07], 2019079: [01, 03, 06, 9, 19, 31, 16],2019078: [01, 17, 27, 29, 31, 33, 12], 2019077: [9, 11, 13, 18, 21, 22, 15],2019076: [12, 15, 19, 20, 29, 32, 14], 2019075: [05, 8, 20, 22, 31, 33, 03],2019074: [12, 21, 27, 29, 31, 33, 04], 2019073: [01, 02, 06, 12, 16, 18, 8],2019072: [02, 9, 13, 15, 22, 30, 15], 2019071: [01, 8, 19, 24, 29, 30, 04]}
     6 doublelist1 = [03, 06, 8, 20, 24, 32,01, 03, 06, 9, 19, 31,01, 17, 27, 29, 31, 33,9, 11, 13, 18, 21, 22,12, 15, 19, 20, 29, 32,05, 8, 20, 22, 31, 33,12, 21, 27, 29, 31, 33,01, 02, 06, 12, 16, 18,02, 9, 13, 15, 22, 30,01, 8, 19, 24, 29, 30]
     7 doublelist2 = {07,16,12,15,14,03,04,8,15,04}
     8 #所有蓝球一个数组
     9 result_dic={}
    10 for item_str in doublelist1:
    11     if item_str not in result_dic:
    12         result_dic[item_str]=1
    13     else:
    14         result_dic[item_str]+=1
    15 print("每个蓝球号码出现的次数集合"+ str(result_dic))
    16 
    17 #所有红球一个数组
    18 
    19 result_dic={}
    20 for item_str in doublelist2:
    21     if item_str not in result_dic:
    22         result_dic[item_str]=1
    23     else:
    24         result_dic[item_str]+=1
    25 print("每个红球号码出现的次数集合"+ str(result_dic))
    26 for i in doublelist:
    27     while True:
    28         guess_num = raw_input("请输入一个值:")
    29         # 判断是不是等于q,等于q退出
    30         if guess_num == 'q':
    31             print("游戏结束")
    32             exit(0)
    33 
    34         # 号码不存在或者非数字提示请重新输入
    35         if guess_num.isdigit():
    36 
    37             # 判断是哪一期的数组
    38             if int(guess_num) == 2019080:
    39                 print ("03, 06, 8, 20, 24, 32, 07")
    40             elif int(guess_num) == 2019079:
    41                 print("01, 03, 06, 9, 19, 31, 16")
    42             elif int(guess_num) == 2019078:
    43                 print("01, 17, 27, 29, 31, 33, 12")
    44             elif int(guess_num) == 2019077:
    45                 print("9, 11, 13, 18, 21, 22, 15")
    46             elif int(guess_num) == 2019076:
    47                 print("12, 15, 19, 20, 29, 32, 14")
    48             elif int(guess_num) == 2019075:
    49                 print("05, 8, 20, 22, 31, 33, 03")
    50             elif int(guess_num) == 2019074:
    51                 print("12, 21, 27, 29, 31, 33, 04")
    52             elif int(guess_num) == 2019073:
    53                 print("01, 02, 06, 12, 16, 18, 8")
    54             elif int(guess_num) == 2019072:
    55                 print("02, 9, 13, 15, 22, 30, 15")
    56             elif int(guess_num) == 2019071:
    57                 print("01, 8, 19, 24, 29, 30, 04")
    58         else:
    59             print ("请重新输入!")
    View Code

    总结:三个人的作业中对计数都用了不同的方法,我的循环遍历的方法使用较差

  • 相关阅读:
    hdu 1686 Oulipo
    [NOI1997] 积木游戏
    错误录——未完待续
    NOI 2014 魔法森林
    hdu 4010 Query on The Trees
    求助大佬6——1种贪心
    51 nod 1205 流水线调度
    bzoj 1180: [CROATIAN2009]OTOCI
    HNOI2010 弹飞绵羊
    SDOI2008 洞穴勘测
  • 原文地址:https://www.cnblogs.com/whycai/p/11561545.html
Copyright © 2020-2023  润新知