求此程式的程式碼 用Dev c++裡面的C專案製作 不是C++喔 程式的功能是 從01到49中任意產生6個號碼不能重複 1.主程式中 輸入6個號碼(你買的號碼) 2.主程式中印出中獎號碼 3.主程式中印出你猜中的號碼 4中獎內容?

2017-06-07 9:47 am

回答 (2)

2017-06-07 11:25 pm
#include <stdio.h>
#include <stdlib.h>

#define N (6)
#define M (50)

int main(){
int c;
char *prize[1+N] = { "You won nothing",
"You won the 6th prize - $1",
"You won the 5th prize - $2",
"You won the 4th prize - $3",
"You won the 3rd prize - $4",
"You won the 2nd prize - $5",
"You won the top prize - $6"};

for(; printf("play again?(1 or 0) ")
&& scanf("%d", &c)
&& 1 == c
;) {
int win[M] = {0}, buy[M] = {0}, i,n;
for(n=1; N>=n
&& printf("Enter number %d (1-49): ",n)
&& scanf("%d", &i)
;)
if(0 < i && i < M && 0 == buy[i]) ++buy[i], ++n;
for(n=1;N>=n ;){
i = 1+(rand()%(M-1));
if(0 == win[i]) ++win[i], ++n;
}
printf("THe winning numbers are:");
for(n=1;M>n;++n) if(win[n]) printf(" %d",n);
printf("\nAnd you matched: ");
for(n=1,i=0;M>n;++n) if(win[n] && buy[n]) ++i, printf(" %d",n);
printf("\nTotal numbers matched= %d\n",i);
printf("\n%s\n",prize[i]);
}
return 0;
}
2017-06-07 2:21 pm
中獎內容沒說明,你自己改吧
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
srand(time(NULL));
int input[6];
int random[6];
int i,j,count = 0;
for(i=0;i<6;i++)
{
random[i]=rand()%49+1;
for(j=0;j<i;j++) if(random[i]==random[j]){ i--; break; }
}
printf("購買號碼: ");
for(i=0;i<6;i++) scanf(" %d",&input[i]);
printf("開獎號碼:");
for(i=0;i<6;i++) printf(" %d",random[i]);
printf("\n中獎號碼:");

for(i=0;i<6;i++)
for(j=0;j<6;j++)
if(input[i] == random[j])
{
printf(" %d",input[i]);
count ++;
break;
}
printf("\n中獎內容:");
switch(count)
{
case 0: printf("%d",count); break;
case 1: printf("%d",count); break;
case 2: printf("%d",count); break;
case 3: printf("%d",count); break;
case 4: printf("%d",count); break;
case 5: printf("%d",count); break;
case 6: printf("%d",count); break;
}
printf("\n"); system("pause"); return 0;
}


收錄日期: 2021-05-04 02:21:03
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20170607014714AA2gWGH

檢視 Wayback Machine 備份