c++ - 匹配两个数组一个用户输入和随机数?
问题描述
问题是用户输入的数字虽然匹配随机数,但匹配来自 1 个两位数的单个数字,例如:
用户输入 6 个数字:
1、2、3、4、5、6
随机生成的数字是:
48、3、24、4、16、1
一共匹配的号码是:4个号码。整数 1、2 和 4 匹配,但整数 2 与整数 24 匹配,为什么因为在 24 内有一个整数 2。我已将它放入数组中,但它仍然匹配。并且有一段时间我生成的随机数重复并仍在运行。
我们的代码应该是一个彩票,您将在其中输入 6 个数字,然后生成 6 个其他随机数。这些必须显示从随机数到用户输入的匹配数。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int size = 6;
const int lotteryDigits = 49;
void generateLottery (int lottoNumbers[])
{
srand(time(0));
for (int j = 0; j < size; j++)
{
int a = 1+(rand()% lotteryDigits);
lottoNumbers[j] = a;
}
for (int i = 1; i <= size; i++)
{
for (int j = i; j <= size; j++)
{
if (lottoNumbers[i] == lottoNumbers[j])
{
lottoNumbers[j] = (rand() % 49 + 2)/ 2;
}
}
}
}
void generatemyLottery (int myLotto[])
{
cout<<"Input your 6 Lucky numbers (1-49)"<<endl;
for (int i = 0; i < size; i++)
{
cin>>myLotto[i];
}
for (int i = 0; i < size; i++)
{
for (int j = i+1; j < size; j++)
{
if (myLotto[i] == myLotto[j] || myLotto[i] > 49 || myLotto[i] < 1)
{
cout<<"\nInvalid Input"<<endl;
}
}
}
}
void matchMaker (int lottoNumbers[], int myLotto[], int hits)
{
for (int i = 1; i <= size; i++)
{
for (int j = 1; j <= size; j++)
{
if (myLotto[i] == lottoNumbers[j])
{
hits++;
}
}
}
}
void displayNumbers (int lottoNumbers[], int myLotto[], int hits, char
yesOrno)
{
cout<<"Do you want to Draw (Y/N): ";
cin>>yesOrno;
if (yesOrno == 'Y' || yesOrno == 'y')
{
if (hits > 1 && hits < 1)
{
cout<<"You hit "<<hits<<" numbers"<<endl;
}
else if (hits == 1 || hits == 0)
{
cout<<"You hit "<<hits<<" number"<<endl;
cout<<"\nBetter Luck next time";
}
else if (hits == 6)
{
cout<<"You got the JACKPOT";
}
cout<<"\nYour numbers are"<<endl;
for (int i = 0; i < size; i++)
{
if (myLotto[i] <= lotteryDigits && myLotto[i]>=1)
{
cout<<myLotto[i]<<" ";
}
}
cout<<"\nWinning number are"<<endl;
for (int i=0; i < size; i++)
{
cout<<lottoNumbers[i]<<" ";
}
cout<<endl;
}
if (yesOrno == 'N' || yesOrno == 'n')
{
cout<<"\nPlease come again"<<endl;
}
}
int main()
{
int lottoNumbers[6];
int myLotto[6];
char yesOrno;
int choice;
int hits = 0;
while (true)
{
cout<<"LOTTO\n\n\n";
cout<<"1. Enter a Number\n";
cout<<"2. Draw the Numbers\n";
cout<<"2. Exit\n\n";
cout<<"Choice: ";
cin>>choice;
if(choice == 1)
{
system ("CLS");
generateLottery (lottoNumbers);
generatemyLottery (myLotto);
system ("PAUSE");
system ("CLS");
}
else if(choice == 2)
{
system ("CLS");
matchMaker (lottoNumbers, myLotto, hits);
displayNumbers (lottoNumbers, myLotto, hits, yesOrno);
system ("PAUSE");
system ("CLS");
}
else if(choice == 3)
{
return 0;
}
else if(choice > 3 || choice < 1 )
{
system ("CLS");
cout<<"Invalid Input\n\n";
system ("PAUSE");
system ("CLS");
}
else{
cout<<" \nInvalid Input";
system ("CLS");
}
}
}
结果应该是
用户输入 6 个数字:
1、2、3、4、5、6
随机生成的数字是:
48、3、24、4、16、1
假定匹配的数字必须只有 3。如果有办法增强它,请随时修改它。
解决方案
您的代码有几个问题。
首先,在generateLottery()
函数中生成随机数时,您不能确保生成的数字是唯一的,这与generatemyLottery()
函数不同,您在函数中检查重复项并在"Invalid Format"
找到任何字符串时打印字符串。也有一些代码改变了generateLottery()
函数中的重复项,但它并不能完全确保数字是唯一的。这可能会导致您在生成的数字列表中有重复的项目,并且可能会导致您在某些输入中获得比您认为应该拥有的更多的命中。
matchMaker()
此外,还有generateLottery()
函数中的越界操作。具体来说,您有for
循环从 index 迭代1
到 index size
,包括在内。由于您的数组是 size 6
,正在访问或修改myLotto[size]
,或者lottoNumbers[size]
将访问您不应该访问的地方。您可能正在写入或读取数组边界之外的值,该值恰好等于用户输入的整数之一,从而错误地增加了您的命中数。
推荐阅读
- c# - How to use AWS CopyObjectRequest when Source and Destination buckets have different AccessKey & Secret
- python - 在 python 字符串中使用带单引号的代码搜索和替换文件文本
- ssl - Hyperledger Fabric SDK - https 和 TLS 证书/密钥
- angular - 角度材质单选按钮在重置后不显示所选值
- javascript - how to tell an element to change its position once it expands outside the viewport?
- python - 正则表达式在字符串对之间删除,但在多对字符串时不删除所有内容
- asp.net - 如何捕获所有组,包括可选的 URL 组?
- angular - Angular:从惰性功能模块添加多提供者
- python-3.x - 在 python 3 中操作命令时忽略反斜杠
- r - Fast greedy on graph objects