我正在尝试创建一个彩票游戏,用户输入10个数字,然后每天播放相同的10个数字一年。我生成365个随机数,模拟一年中每一天的一个新数字。我的菜单中需要案例4的帮助。如何显示365个随机数中最常出现的数字?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define PAUSE system("pause")
#define SIZE 365
#define FLUSH fflush(stdin)
#define CLS system("cls")
#define LB 0
#define UB 999
main(){
int userInput = 0;
int userNumber = 0;
int count = 0;
int userNumbers[9] = { 0 };
int i;
int j;
int f;
int winningNumbers[SIZE] = { 0 };
int moneySpent = 3600;
int moneyEarned = 0;
int profitMade = 0;
int timesWon = 0;
int matchingNumbers[SIZE] = { 0 };
int userNumberOne = 0;
int userNumberTwo = 0;
int userNumberThree = 0;
int userNumberFour = 0;
int userNumberFive = 0;
int userNumberSix = 0;
int userNumberSeven = 0;
int userNumberEight = 0;
int userNumberNine = 0;
int userNumberTen = 0;
int a = 0;
do {
// Display Menu
CLS;
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
printf("$$ $$\n");
printf("$$ THE LOTTERY GAME $$\n");
printf("$$ $$\n");
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n\n");
printf("1. Enter your 10 lottery numbers. \n");
printf("2. Play the lottery. \n");
printf("3. Display summary of your 10 numbers. \n");
printf("4. Display most repeated number. \n");
printf("5. QUIT \n");
printf("\n\nEnter your selection: ");
// Get User Choice
scanf("%i", &userInput);
CLS;
//Write the Switch
switch (userInput){
srand((unsigned)time(NULL)); // Seeds the rand function
case 1: // User inputs 10 lottery numbers to play
for (count; count < 10; count++) { //Loops 10 times
printf("\nEnter #%i of 10 for your lottery numbers [BETWEEN 0 - 999]: ", count + 1); // Tells user to input 10 numbers
scanf("%i", &userNumber); // Gets users' 10 lottery numbers
userNumbers[count] = userNumber; // Stores the users' lottery numbers into an array
} // End for loop
PAUSE;
break;
case 2: // Plays the lottery
for (i = 0; i < SIZE; i++){
winningNumbers[i] = LB + rand() % (UB - LB + 1); // Generates 365 random lottery numbers
} // End for loop
for (j = 0; j < SIZE; j++){
if (userNumbers[0] == winningNumbers[j]) {
matchingNumbers[timesWon] = userNumbers[0];
timesWon++;
userNumberOne++;
}
else if (userNumbers[1] == winningNumbers[j]) {
matchingNumbers[timesWon] = userNumbers[1];
timesWon++;
userNumberTwo++;
}
else if (userNumbers[2] == winningNumbers[j]) {
matchingNumbers[timesWon] = userNumbers[2];
timesWon++;
userNumberThree++;
}
else if (userNumbers[3] == winningNumbers[j]) {
matchingNumbers[timesWon] = userNumbers[3];
timesWon++;
userNumberFour++;
}
else if (userNumbers[4] == winningNumbers[j]) {
matchingNumbers[timesWon] = userNumbers[4];
timesWon++;
userNumberFive++;
}
else if (userNumbers[5] == winningNumbers[j]) {
matchingNumbers[timesWon] = userNumbers[5];
timesWon++;
userNumberSix++;
}
else if (userNumbers[6] == winningNumbers[j]) {
matchingNumbers[timesWon] = userNumbers[6];
timesWon++;
userNumberSeven++;
}
else if (userNumbers[7] == winningNumbers[j]) {
matchingNumbers[timesWon] = userNumbers[7];
timesWon++;
userNumberEight++;
}
else if (userNumbers[8] == winningNumbers[j]) {
matchingNumbers[timesWon] = userNumbers[8];
timesWon++;
userNumberNine++;
}
else if (userNumbers[9] == winningNumbers[j]) {
matchingNumbers[timesWon] = userNumbers[9];
timesWon++;
userNumberTen++;
}
} // End for loop
printf("You WON the lottery %i times!\n\n", timesWon);
printf("Your winning numbers are: \n");
for (f = 0; f < timesWon; f++){
printf("%i \n", matchingNumbers[f]);
} // End for loop
moneyEarned = 500 * timesWon;
profitMade = moneyEarned - moneySpent;
printf("\n\n");
printf("You WON $%i total!\n", moneyEarned);
printf("You spent $%i total.\n", moneySpent);
printf("Your profit is $%i! \n\n", profitMade);
PAUSE;
break;
case 3: // Displays summary of the user's 10 numbers
printf("$$$$ Your lottery number summary $$$$\n\n");
printf("Lottery number %i won %i times.\n", userNumbers[0], userNumberOne);
printf("Lottery number %i won %i times.\n", userNumbers[1], userNumberTwo);
printf("Lottery number %i won %i times.\n", userNumbers[2], userNumberThree);
printf("Lottery number %i won %i times.\n", userNumbers[3], userNumberFour);
printf("Lottery number %i won %i times.\n", userNumbers[4], userNumberFive);
printf("Lottery number %i won %i times.\n", userNumbers[5], userNumberSix);
printf("Lottery number %i won %i times.\n", userNumbers[6], userNumberSeven);
printf("Lottery number %i won %i times.\n", userNumbers[7], userNumberEight);
printf("Lottery number %i won %i times.\n", userNumbers[8], userNumberNine);
printf("Lottery number %i won %i times.\n\n\n", userNumbers[9], userNumberTen);
PAUSE;
break;
case 4: // Displays most repeated number
PAUSE;
break;
case 5: // QUIT
printf("Thank you for playing the lottery game!\n\n");
break;
}//End Switch
} while (userInput != 5);
PAUSE;
} // End main
答案 0 :(得分:2)
这有效,请阅读代码中的// NOTE
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define PAUSE system("pause")
#define SIZE 365
#define FLUSH fflush(stdin)
#define CLS system("cls")
#define LB 0
#define UB 999
int main( )
{
int userInput = 0;
int userNumber = 0;
int count = 0;
int userNumbers[9 + 1] = { 0 };
int i;
int j;
int f;
int winningNumbers[SIZE] = { 0 };
int moneySpent = 3600;
int moneyEarned = 0;
int profitMade = 0;
int timesWon = 0;
int matchingNumbers[SIZE] = { 0 };
int userNumberOne = 0;
int userNumberTwo = 0;
int userNumberThree = 0;
int userNumberFour = 0;
int userNumberFive = 0;
int userNumberSix = 0;
int userNumberSeven = 0;
int userNumberEight = 0;
int userNumberNine = 0;
int userNumberTen = 0;
int a = 0;
// NOTE +1 needed below
int lotnum[UB + 1] = { 0 };
// NOTE some new variables here
int most_repeated = 0;
int tempint = 0;
do
{
// Display Menu
CLS;
printf( "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n" );
printf( "$$ $$\n" );
printf( "$$ THE LOTTERY GAME $$\n" );
printf( "$$ $$\n" );
printf( "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n\n" );
printf( "1. Enter your 10 lottery numbers. \n" );
printf( "2. Play the lottery. \n" );
printf( "3. Display summary of your 10 numbers. \n" );
printf( "4. Display most repeated number. \n" );
printf( "5. QUIT \n" );
printf( "\n\nEnter your selection: " );
// Get User Choice
scanf( "%i", &userInput );
CLS;
srand( ( unsigned ) time( NULL ) ); // Seeds the rand function
//Write the Switch
switch ( userInput )
{
case 1: // User inputs 10 lottery numbers to play
for ( count; count < 10; count++ )
{ //Loops 10 times
printf( "\nEnter #%i of 10 for your lottery numbers [BETWEEN 0 - 999]: ", count + 1 ); // Tells u ser to input 10 numbers
scanf( "%i", &userNumber ); // Gets users' 10 lottery numbers
userNumbers[count] = userNumber; // Stores the users' lottery numbers into an array
} // End for loop
PAUSE;
break;
case 2: // Plays the lottery
for ( i = 0; i < SIZE; i++ )
{
winningNumbers[i] = LB + rand( ) % ( UB - LB + 1 ); // Generates 365 random lottery numbers
printf( "\nwN==%d", winningNumbers[i] );
} // End for loop
for ( j = 0; j < SIZE; j++ )
{
if ( userNumbers[0] == winningNumbers[j] )
{
matchingNumbers[timesWon] = userNumbers[0];
timesWon++;
userNumberOne++;
}
else if ( userNumbers[1] == winningNumbers[j] )
{
matchingNumbers[timesWon] = userNumbers[1];
timesWon++;
userNumberTwo++;
}
else if ( userNumbers[2] == winningNumbers[j] )
{
matchingNumbers[timesWon] = userNumbers[2];
timesWon++;
userNumberThree++;
}
else if ( userNumbers[3] == winningNumbers[j] )
{
matchingNumbers[timesWon] = userNumbers[3];
timesWon++;
userNumberFour++;
}
else if ( userNumbers[4] == winningNumbers[j] )
{
matchingNumbers[timesWon] = userNumbers[4];
timesWon++;
userNumberFive++;
}
else if ( userNumbers[5] == winningNumbers[j] )
{
matchingNumbers[timesWon] = userNumbers[5];
timesWon++;
userNumberSix++;
}
else if ( userNumbers[6] == winningNumbers[j] )
{
matchingNumbers[timesWon] = userNumbers[6];
timesWon++;
userNumberSeven++;
}
else if ( userNumbers[7] == winningNumbers[j] )
{
matchingNumbers[timesWon] = userNumbers[7];
timesWon++;
userNumberEight++;
}
else if ( userNumbers[8] == winningNumbers[j] )
{
matchingNumbers[timesWon] = userNumbers[8];
timesWon++;
userNumberNine++;
}
else if ( userNumbers[9] == winningNumbers[j] )
{
matchingNumbers[timesWon] = userNumbers[9];
timesWon++;
userNumberTen++;
}
} // End for loop
printf( "You WON the lottery %i times!\n\n", timesWon );
printf( "Your winning numbers are: \n" );
for ( f = 0; f < timesWon; f++ )
{
printf( "%i \n", matchingNumbers[f] );
} // End for loop
moneyEarned = 500 * timesWon;
profitMade = moneyEarned - moneySpent;
printf( "\n\n" );
printf( "You WON $%i total!\n", moneyEarned );
printf( "You spent $%i total.\n", moneySpent );
printf( "Your profit is $%i! \n\n", profitMade );
PAUSE;
break;
case 3: // Displays summary of the user's 10 numbers
printf( "$$$$ Your lottery number summary $$$$\n\n" );
printf( "Lottery number %i won %i times.\n", userNumbers[0],
userNumberOne );
printf( "Lottery number %i won %i times.\n", userNumbers[1],
userNumberTwo );
printf( "Lottery number %i won %i times.\n", userNumbers[2],
userNumberThree );
printf( "Lottery number %i won %i times.\n", userNumbers[3],
userNumberFour );
printf( "Lottery number %i won %i times.\n", userNumbers[4],
userNumberFive );
printf( "Lottery number %i won %i times.\n", userNumbers[5],
userNumberSix );
printf( "Lottery number %i won %i times.\n", userNumbers[6],
userNumberSeven );
printf( "Lottery number %i won %i times.\n", userNumbers[7],
userNumberEight );
printf( "Lottery number %i won %i times.\n", userNumbers[8],
userNumberNine );
printf( "Lottery number %i won %i times.\n\n\n", userNumbers[9],
userNumberTen );
PAUSE;
break;
case 4: // Displays most repeated number
for ( i = 0; i < SIZE; i++ )
{
tempint = winningNumbers[i]; // NOTE could be shorter, but this is easier to read
lotnum[tempint]++;
printf( "\n lotnum[%d]==%d", tempint, lotnum[tempint] ); // NOTE switch off this line after you understand it
}
most_repeated = 0;
for ( i = 0; i < UB; i++ )
if ( lotnum[i] > most_repeated )
most_repeated = i;
printf( "\nMost repeated number==%d\n", most_repeated );
// NOTE : you could have multiple numbers with same score
PAUSE;
break;
case 5: // QUIT
printf( "Thank you for playing the lottery game!\n\n" );
break;
} //End Switch
}
while ( userInput != 5 );
PAUSE;
} // End main