python練習 學生班級分配問題

2021-10-05 08:44:41 字數 1618 閱讀 7307

題目要求四個班級,八個學生,隨機將八個學生平均分配到四個班級中,也就是每乙個班級有且只有兩個學生。

import  random

classes=[[

],,,

]students=

['a'

,'b,'

,'c'

,'d'

,'e'

,'f'

,'g'

,'h'

]index=random.randint(0,

3)classes[index]0]

)flag=

0while flag<7:

for stu in students:

index = random.randint(0,

3)iflen

(classes[index]

)<2:

i=0while i<4:

if stu in classes[i]

:break

i+=1else

: classes[index]

flag +=1i=

1for temp in classes:

print

('班級%d的人數為:%d'

%(i,

len(temp)))

i+=1for stu in temp:

print

('%s'

%stu,end=

' ')

print

('-'*20

)

實現方式2:

import  random

classes=[[

],,,

]students=

['a'

,'b,'

,'c'

,'d'

,'e'

,'f'

,'g'

,'h'

]for stu in students:

index=random.randint(0,

3)iflen

(classes[index])==

2:while

true

:if index==3:

iflen

(classes[0]

)<2:

break

else

: index=

0else

: index+=1if

len(classes[index]

)<2:

break

classes[index]i=1

for temp in classes:

print

('班級%d的人數為:%d'

%(i,

len(temp)))

i+=1for stu in temp:

print

('%s'

%stu,end=

' ')

print

('-'*20

)

練習 學生班級

create table t class c id int primary key,c name varchar 10 insert into t class values 1,一班 insert into t class values 2,二班 insert into t class values...

班級資訊的學生類

include include using namespace std class stu 宣告基類 void display protected 訪問許可權為保護型的資料成員 int num 學生學號 string name 學生姓名 class studetail public stu 宣告派生...

學生學號判斷專業班級

學生的學號為12位,其組成為 年份 2位 專業資訊 6位 班級 2位 班內序號 2位 例如學號 111909030208 11 代表2011年入學,190903 代表網路工程專業,02 代表在網路工程專業的02班,08 為班內序號。已知資訊工程系三個專業的 為 190901 電腦科學與技術專業 19...