2012-02-02 2 views
1

나는 insta가 로또 번호를 고르는 앱을 가지고 있습니다. 배열을 추가하려고 시도했지만 Bubble 메서드를 시도했지만 코드에 통합 할 위치를 모른다. 여러 번 시도하고 여러 가지 방법을 시도했지만 오류가 계속 발생합니다. 어떤 충고?랜덤 넘버 생성기는 C++에서 가장 낮은 것부터 가장 높은 것까지 어떻게 올 수 있습니까?

/*This app will give you your lotto max numbers!*/ 
#include <iostream> 
#include <iomanip> 
#include <cstdlib> 
using namespace std; 

int main() 
{ 
    int lotto[7]; 
    //allows users to input a seed, that way the tickets wont be 
    //generated the same if they want two. 
    unsigned seed; 
    cout << "Enter seed "; 
    cin >> seed; 
    srand(seed) ; 

    //greeting 
    cout << "*** LOTTO MAX INSTA PICK ***" << endl; 
    cout << " " << endl << endl; 

    cout << "Your Insta Pick Numbers" << endl; 
    cout<< " " << endl << endl; 

    //initilizes the counter and making sure the amount of numbers does not exceed 21 
    for (int counter = 1; counter <= 7;++ counter) 
    { 
     //outputs numbers between 1 and 27 with 7 numbers in each row 
     cout << setw(1) << (1 + rand() % 49) << " " ; 
     //stops the counter for each row 
     if (counter % 7 == 0) 
      cout << endl; 
    } 

    //space in between the numbers and tag numbers 
    cout<< " " << endl << endl; 
    cout << "Your Tag Numbers" << endl; 
    cout << " " << endl << endl; 

    //initilizes counter and makes sure numbers dont go over 6 
    for (int counter = 1; counter <= 6; ++ counter) 
    { 
     //sets the numbers between 0 and 9 and outputs them 
     cout << setw(0) << (0 + rand() % 9)<< " " ; 

     //stops the counter 
     if (counter % 9 == 0) 
      cout << endl; 
    } 

    cout << " " << endl << endl; 

    //end message 
    cout << "Thank you for playing!! please check ticket\n a year minus a day from date of purchase" <<endl; 
}; 
+0

오류 메시지가 무엇인가요? – CoffeeRain

+0

무엇이 오류입니까? –

+0

들여 쓰기를 먼저 수정하십시오. –

답변

2
std::set<int> numbers; 
while(numbers.size() < 7) 
{ 
    numbers.insert((rand() % 49) + 1); 
} 

세트에는 고유 번호가 저장되므로 크기가 7이 될 때까지 난수를 계속 삽입합니다. 어떤 중복이 나오면 세트의 크기가 증가하지 않습니다.

+0

Please 이 코드가 작동하는 이유에 대한 설명을 제공하십시오. –

+1

http://search.dilbert.com/comic/Random%20Number%20Generator –

4

선택한 임의의 숫자를 저장 한 다음 출력하기 전에 정렬해야합니다. 저장 장소를 이미 정의한 것처럼 보입니다. 사용하기 만하면됩니다.

같은 난수를 두 번 얻은 경우 어떻게 될지 생각할 수도 있습니다.

+0

네, 저의 로또를 가지고 있는데, if 문에서 abouts라고 부릅니다. –

+0

나는 시드 함수를 가지고 있기 때문에 같은 숫자를 생성하지 않을 것이다. –

+0

씨앗은 동일한 시퀀스의 숫자를 얻는 것을 멈추게 할 것이다. (사용자가 매번 다른 시드를주는 경우 .1, 3, 5와 같은 중복을 막지 않을 것이다. , 5, 20, 7, 7' – IanGilham

1

숫자를 모자에서 그려서 작성해야하는 경우 손으로 복권을 작성하는 방법을 생각해보십시오. 숫자는 무작위로 그려지기 때문에 그 숫자를 얻으면 숫자를 쓸 수 없습니다. 대신, 배열 (배열)을 모두 선택하기 전까지는 배열을 측면으로 유지해야합니다. 그런 다음 숫자 정렬 (배열 정렬)을 할 수 있습니다. 이제 각 숫자를 정렬 된 배열에서 순서대로 작성할 수 있습니다.

마크 랜섬 (Mark Ransom)이 언급했듯이 모자에서 같은 번호를 두 번 당기는 경우 어떻게되는지 고려해야합니다.

관련 문제