[語法] 大樂透電腦選號
今天上課的時候教亂數然後要大家試著寫出讓電腦選號...
後來做完後我發現數字會重複...
老師說以後才會教到...
所以我自己想辦法解決了...
原本是用兩個for...
用第二個for來產生6個亂數...
我把第二個for刪除把六個亂數指定給六個變數...
在用if但寫了很長...
以前上課有說寫程式最好部要用goto因為會比較難看...
跳來跳去的...
要用什麼與法來解決這個問題?
下面是我寫的程式碼...
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int c,a,q,w,e,r,t;
srand(time(NULL));
cout << "你要買幾組:";
cin >> a;
for(int i=1;i<=a;i++){
cout<<"第"<< i << "組號碼為:";
asd:
q=rand()%49;q++;
w=rand()%49;w++;
e=rand()%49;e++;
r=rand()%49;r++;
t=rand()%49;t++;
c=rand()%49;c++;
if(q==w||q==e||q==r||q==t||w==e||w==r||w==t||e==r||e==t||r==t||c==q||c==w||c==e||c==e||c==r||c==t)
goto asd;
cout <<q<<' '<<w<<' '<<e<<' '<<r<<' '<<t<<' '<<c<<"\n";
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.240.248
※ 編輯: abc505054 來自: 122.116.240.248 (11/20 22:48)
推
11/20 22:55, , 1F
11/20 22:55, 1F
推
11/20 23:03, , 2F
11/20 23:03, 2F
推
11/22 10:22, , 3F
11/22 10:22, 3F
推
11/23 13:00, , 4F
11/23 13:00, 4F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章