본문 바로가기
프로그래밍 문제/c++ 문제

c++ 문제 30. 로또 번호 생성

by 지나팩 2023. 11. 8.

로또 번호를 생성하여 출력하는 코드를 작성하세요. 번호는 중복되어선 안됩니다.

출력 예)

코드는 아래에~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;
 
int main()
{    
    std::srand(static_cast<unsigned int>(std::time(nullptr)));
 
    int lotto[6= { 0 };
    int count = 0;
    bool isSame = false;
 
    while (true)
    {
        isSame = false;
        lotto[count] = std::rand() % 45 + 1;
        if (count > 0)
        {
            for (int i = 0; i < count; i++)
            {
                if (lotto[i] == lotto[count]) {
                    isSame = true;
                    break;
                }
            }
        }
        
        if(!isSame)count++;        
 
        if (count == 6break;
    }
        
    for (int i = 0; i < sizeof(lotto) / sizeof(*lotto); i++)
    {
        cout << "로또 번호 " << i + 1 << ":" << lotto[i] << endl;
    }
 
    return 0;
}
cs

댓글