C-언어 로또복권 추첨 프로그램 |
|||||
---|---|---|---|---|---|
작성자 | 수안보중학교 | 등록일 | 19.09.06 | 조회수 | 10 |
// 로또복권 1등 당첨된 번호를 컴퓨터가 몇번 난수를 발생했을때 같은번호가 나오는가 실험해 봅시다. 돌린횟수는? 시간은? #include <stdio.h> #include <stdlib.h> #include <time.h> #include <algorithm> // sort(); using namespace std; int main() { int i,n[6],j,cnt=0; int max=-9999; int min=9999; srand(time(NULL)); do { for (i=0; i<6; i++) { n[i]=rand()%45+1; for(j=0; j<i; j++) { if(n[i]==n[j]) { i--; //중복검사 } } } sort(n,n+6); //정렬 for(i=0; i<6; i++) { printf("%4d",n[i]); if(max<n[i]) { max=n[i]; } if(min>n[i]) { min=n[i]; } } printf("\n"); cnt++; }while(n[0]!=3 || n[1]!=14 || n[2]!=20 || n[3]!=35 || n[4]!=39 || n[5]!=43); //3,14,20,35,39,43 이 나올때까지 무한 반복 printf("\n최대값 : %d\n최솟값 : %d\n돌린횟수:%d",max,min,cnt); }
|
이전글 | '안녕하세요?' 글자를 화면에 출력시키는 프로그램을 작성하시오.(C 언어) |
---|