[討論] 小小的中獎程式

看板Programming作者 (我的上帝)時間16年前 (2007/11/26 23:51), 編輯推噓2(202)
留言4則, 2人參與, 最新討論串1/2 (看更多)
題目--- 寫一個程式, 利用隨機產生六個樂透號碼和一個特別號 (0<x<45) -->Input <6 個整數 > --> Output < 有中獎的號碼 > ***************************** -->Sample Input 11, 25, 37, 9, 12, 31 -->Sample Output 25, 37, 31 ***************************** 提示-- 使用函式庫time與srand、rand()兩個函式來取出亂數, 並控制好輸出值的範圍,再與輸入的數字進行比較。 然後 關於一個程式的問題... 程式碼如下 #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int n1[7],n2[6],i,j; //設變數n1為開獎號碼,n2為使用者所選擇號碼 srand((unsigned)time(NULL)); //重新選擇亂數種子數 for(j=0;j<7;j++) //取7個亂數,亂數範圍1~44 { n1[j]=rand()%44+1; if(n1[j]==n1[j-1]) { for(j=0;j<7;j++) { n1[j]=rand()%44+1; } } } printf("請輸入六個不同樂透號碼(1~44):\n"); for(i=0;i<6;i++) //讀取使用者輸入的六個樂透號碼 { printf("第%d個號碼:",i+1); scanf("%d",&n2[i]); } printf("\n"); printf("本期開獎號碼:"); for(j=0;j<7;j++) //印出本期開獎七個號碼(含特別號) { printf("%d ",n1[j]); } printf("\n"); printf("中獎號碼:"); for(j=0;j<7;j++) //從開獎號碼中找出中獎號碼 { for(i=0;i<6;i++) { if(n1[j]==n2[i]) printf("%d ",n2[i]); } } printf("\n"); system("PAUSE"); return 0; } ************************************************************** 請問有辦法不用到if就寫出來嗎??? *************************************************************** 不知道有沒有違反板規... 第一次在這裡po文....... -- 【C party】!!寫程式全民運動~~ http://www.wretch.cc/blog/Cparty -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.126.172.79

11/27 00:11, , 1F
可以啊 回圈控制裡面都有隱含著判斷
11/27 00:11, 1F

11/27 00:12, , 2F
或著用 && || 運算子的特性去做也可以
11/27 00:12, 2F

11/27 00:13, , 3F
n1[j]==n2[i] && printf("%d",n2[i]);
11/27 00:13, 3F

11/27 12:32, , 4F
程式縮排是個好習慣
11/27 12:32, 4F
文章代碼(AID): #17Ikk36r (Programming)
文章代碼(AID): #17Ikk36r (Programming)