python 编写一个彩票游戏?

python 编写一个彩票游戏?,第1张

按照题目要求编写的Python程序如下

import random

numlist=random.sample(range(0,10),5)

while numlist[0]==0:

  numlist=random.sample(range(0,10),5)

num=int(''.join([str(i) for i in numlist]))

inputnum=int(input("输入号:"))

bonus=0

count=0

if inputnum==num:

bonus=10000

else:

for i in set(str(inputnum)):

if int(i) in numlist:

count+=1

bonus=1000*count

print("彩票号:%d" % num)

print("奖金:%d元" % bonus)

源代码(注意源代码的缩进)

新建一个单窗体的工程,在上面画出七个TEXTBOX,最好是一个控件数组,这样编程时容易控制,再建一个COMMOND BUTTON,将CAPTION改为“随机产生”。在程序运行后,每点击一下COMMAND1,将随机产生一组数字并按从小到大的顺序显示在文本框中。下面就是程序部分:Dim NumArray(1 To 7) As Integer′通用中定义

Private Sub Command1_Click()

Dim i, j, N As Integer

For i = 1 To 7

NumArray(i) = 0

Next i

Randomize

NumArray(1) = Fix(1 + 32 * (Rnd()))

j = 1

Do

N = Fix(1 + 32 * (Rnd()))

For i = 1 To j

If N = NumArray(i) Then

Exit For ′重复时

ElseIf i = j Then ′未重复时

NumArray(i + 1) = N

j = j + 1

Exit For

End If

Next i

Loop While j <7

PopSort ′升序排列

For i = 1 To 7

Text1(i - 1).Text = NumArray(i)

Next i

End Sub

Private Sub PopSort() ′气泡排序法

Dim i, j, Temp As Integer

For i = 7 To 2 Step -1

For j = 7 - 1 To 1 Step -1

If i >= 7 - j + 1 Then

If NumArray(j + 1) <NumArray(j) Then

Temp = NumArray(j)

NumArray(j) = NumArray(j + 1)

NumArray(j + 1) = Temp

End If

End If

Next j

Next i

End Sub

#include<stdio.h>

void main()

{

int a[]={1,2,3,4,5,6,7}

int b[7],i,j,count=0,max=0,maxflag

    for(i=0i<7i++)

scanf("%d",&b[i])

    i=0

while(i<7)

{

j=0

count=0

if(b[i]==a[j])

while(i<7 && j<7 && b[i]==a[j]) i++,j++,count++

else

i++

if(max<count)  {max =countmaxflag=i}

}

count = max

if(count==7) printf("特等奖\n")

else if(count==6)

{

if(maxflag==7)

printf("二等奖\n")

else

printf("一等奖\n")

}

else if(count==5)

{

if(maxflag==7)

printf("三等奖\n")

else

printf("二等奖\n")

}

else if(count==4)

{

if(maxflag==7)

printf("四等奖\n")

else

printf("三等奖\n")

}

else if(count==3)

{

if(maxflag==7)

printf("五等奖\n")

else

printf("四等奖\n")

}

else if(count==2 && maxflag!=7)

printf("五等奖\n")

else 

printf("没中奖\n")

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8139545.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-13
下一篇2023-04-13

发表评论

登录后才能评论

评论列表(0条)

    保存