在算法书上看到这样一道题,我觉得很有意思,但是却没有办法搞出一个比较高效的方法。所以就选择用程序员的暴力美学来做。好孩子不要学,现在跑还来得及。。。。。
请回答下面10个问题,各题都恰有一个答案是正确的:
(1)第一个答案是B的问题是哪一个?
A、2
B、3
C、4
D、5
E、6
(2)恰好有两个连续问题的答案是一样的,它们是:
A、2,3
B、3,4
C、4,5
D、5,6
E、6,7
(3)本问题答案和哪一个问题的答案相同?
A、1
B、2
C、4
D、7
E、6
(4)答案是A的问题的个数是:
A、0
B、1
C、2
D、3
E、4
(5)本问题答案和哪一个问题的答案相同?
A、10
B、9
C、8
D、7
E、6
(6)答案是A的问题的个数和答案是什么的问题的个数相同?
A、B
B、C
C、D
D、E
E、以上都不是
(7)按照字母顺序,本问题的答案和下一个问题的答案相差几个字母?
A、4
B、3
C、2
D、1
E、0(注:A和B相差一个字母)
(8)答案是元音字母的问题的个数是:
A、2
B、3
C、4
D、5
E、6(注:A和E是元音字母)
(9)答案是辅音字母的问题的个数是:
A、一个质数
B、一个阶乘数
C、一个平方数
D、一个立方数
E、5的倍数
(10)本问题的答案是:
A、A
B、B
C、C
D、D
E、E
我用python把题目直译了,需要做的准备工作就是统计一下答案个数,还有就是第9题
需要算一下十以内的符合的数,第2题
第一次做的时候忽略了“恰好”,所以导致多了答案,回头修改的时候,发现这个写法是真的丑陋,但是还是挺整齐的,逃。。。。
def get_num(mydic):
num_of_dic = {}
num_of_dic['a']=0
num_of_dic['b']=0
num_of_dic['c']=0
num_of_dic['d']=0
num_of_dic['e']=0
for k in mydic:
num_of_dic[mydic[k]]=1+num_of_dic[mydic[k]]
return num_of_dic
def judge(dic):
flag = True
#1
if dic['1']=='a':
flag = flag and (dic['1']!='b' and dic['2']=='b')
if dic['1']=='b':
flag = flag and dic['1']!='b' and dic['2']!='b' and dic['3']=='b'
if dic['1']=='c':
flag = flag and dic['1']!='b' and dic['2']!='b'and dic['3']!='b' and dic['4']=='b'
if dic['1']=='d':
flag = flag and dic['1']!='b' and dic['2']!='b'and dic['3']!='b' and dic['4']!='b' and dic['5']=='b'
if dic['1']=='e':
flag = flag and dic['1']!='b' and dic['2']!='b'and dic['3']!='b' and dic['4']!='b' and dic['5']!='b'and dic['6']=='b'
if flag == False:
return flag
#2
if dic['2']=='a':
flag = flag and (
dic['2']==dic['3'] and
dic['1']!=dic['2'] and
dic['3']!=dic['4'] and
dic['4']!=dic['5'] and
dic['5']!=dic['6'] and
dic['6']!=dic['7'] and
dic['7']!=dic['8'] and
dic['8']!=dic['9'] and
dic['9']!=dic['10'])
if dic['2']=='b':
flag = flag and (
dic['3']==dic['4'] and
dic['1']!=dic['2'] and
dic['2']!=dic['3'] and
dic['4']!=dic['5'] and
dic['5']!=dic['6'] and
dic['6']!=dic['7'] and
dic['7']!=dic['8'] and
dic['8']!=dic['9'] and
dic['9']!=dic['10'])
if dic['2']=='c':
flag = flag and (
dic['4']==dic['5'] and
dic['1']!=dic['2'] and
dic['3']!=dic['4'] and
dic['2']!=dic['3'] and
dic['5']!=dic['6'] and
dic['6']!=dic['7'] and
dic['7']!=dic['8'] and
dic['8']!=dic['9'] and
dic['9']!=dic['10'])
if dic['2']=='d':
flag = flag and (
dic['5']==dic['6'] and
dic['1']!=dic['2'] and
dic['3']!=dic['4'] and
dic['4']!=dic['5'] and
dic['2']!=dic['3'] and
dic['6']!=dic['7'] and
dic['7']!=dic['8'] and
dic['8']!=dic['9'] and
dic['9']!=dic['10'])
if dic['2']=='e':
flag = flag and (
dic['6']==dic['7'] and
dic['1']!=dic['2'] and
dic['3']!=dic['4'] and
dic['4']!=dic['5'] and
dic['5']!=dic['6'] and
dic['2']!=dic['3'] and
dic['7']!=dic['8'] and
dic['8']!=dic['9'] and
dic['9']!=dic['10'] )
if flag == False:
return flag
#3
if dic['3']=='a':
flag = flag and dic['1']==dic['3']
if dic['3']=='b':
flag = flag and dic['2']==dic['3']
if dic['3']=='c':
flag = flag and dic['4']==dic['3']
if dic['3']=='d':
flag = flag and dic['7']==dic['3']
if dic['3']=='e':
flag = flag and dic['6']==dic['3']
if flag == False:
return flag
#4
if dic['4']=='a':
flag = flag and num_ans['a']==0
if dic['4']=='b':
flag = flag and num_ans['a']==1
if dic['4']=='c':
flag = flag and num_ans['a']==2
if dic['4']=='d':
flag = flag and num_ans['a']==3
if dic['4']=='e':
flag = flag and num_ans['a']==4
if flag == False:
return flag
#5
if dic['5']=='a':
flag = flag and dic['5']==dic['10']
if dic['5']=='b':
flag = flag and dic['5']==dic['9']
if dic['5']=='c':
flag = flag and dic['5']==dic['8']
if dic['5']=='d':
flag = flag and dic['5']==dic['7']
if dic['5']=='e':
flag = flag and dic['5']==dic['6']
if flag == False:
return flag
#6
if dic['6']=='a':
flag = flag and num_ans['a']==num_ans['b']
if dic['6']=='b':
flag = flag and num_ans['a']==num_ans['c']
if dic['6']=='c':
flag = flag and num_ans['a']==num_ans['d']
if dic['6']=='d':
flag = flag and num_ans['a']==num_ans['e']
if dic['6']=='e':
flag = flag and (num_ans['a'] not in [num_ans['b'],num_ans['c'],num_ans['d'],num_ans['e']])
if flag == False:
return flag
#7
if dic['7']=='a':
flag = flag and dic['8']=='e'
if dic['7']=='b':
flag = flag and dic['8']=='e'
if dic['7']=='c':
flag = flag and (dic['8']=='e' or dic['8']=='a')
if dic['7']=='d':
flag = flag and (dic['8']=='e' or dic['8']=='c')
if dic['7']=='e':
flag = flag and dic['8']=='e'
if flag == False:
return flag
#8
if dic['8']=='a':
flag = flag and num_ans['a']+num_ans['e']==2
if dic['8']=='b':
flag = flag and num_ans['a']+num_ans['e']==3
if dic['8']=='c':
flag = flag and num_ans['a']+num_ans['e']==4
if dic['8']=='d':
flag = flag and num_ans['a']+num_ans['e']==5
if dic['8']=='e':
flag = flag and num_ans['a']+num_ans['e']==6
if flag == False:
return flag
#9
if dic['9']=='a':
flag = flag and (num_ans['b']+num_ans['c']+num_ans['d'] in [2,3,5,7])
if dic['9']=='b':
flag = flag and (num_ans['b']+num_ans['c']+num_ans['d'] in [1,2,6] )
if dic['9']=='c':
flag = flag and (num_ans['b']+num_ans['c']+num_ans['d'] in [1,4,9])
if dic['9']=='d':
flag = flag and (num_ans['b']+num_ans['c']+num_ans['d'] in[1,8])
if dic['9']=='e':
flag = flag and (num_ans['b']+num_ans['c']+num_ans['d'] in [0,5,10])
if flag == False:
return flag
#10
if dic['10']=='a':
flag = flag and dic['10']=='a'
if dic['10']=='b':
flag = flag and dic['10']=='b'
if dic['10']=='c':
flag = flag and dic['10']=='c'
if dic['10']=='d':
flag = flag and dic['10']=='d'
if dic['10']=='e':
flag = flag and dic['10']=='e'
return flag
dic = {}
for q1 in ['a','b','c','d','e']:
dic['1']=q1
for q2 in ['a','b','c','d','e']:
dic['2']=q2
for q3 in ['a','b','c','d','e']:
dic['3']=q3
for q4 in ['a','b','c','d','e']:
dic['4']=q4
for q5 in ['a','b','c','d','e']:
dic['5']=q5
for q6 in ['a','b','c','d','e']:
dic['6']=q6
for q7 in ['a','b','c','d','e']:
dic['7']=q7
for q8 in ['a','b','c','d','e']:
dic['8']=q8
for q9 in ['a','b','c','d','e']:
dic['9']=q9
for q10 in ['a','b','c','d','e']:
dic['10']=q10
num_ans=get_num(dic)
if judge(dic):
print("*******************")
print(dic)
print(num_ans)
print("*******************")
# dic['1']='c'
# dic['2']='d'
# dic['3']='e'
# dic['4']='b'
# dic['5']='e'
# dic['6']='e'
# dic['7']='d'
# dic['8']='c'
# dic['9']='b'
# dic['10']='a'
# num_ans=get_num(dic)
# if judge(dic):
# print(dic)
网友评论