本帖最后由 hkivanleeon68 于 2018-10-7 14:16 编辑
学习写的 分享给大家 欢迎讨论
喜欢的可以拿回去自己生成玩一下
日后会慢慢根据学习更新
[C++] 纯文本查看 复制代码 #include <iostream>
#include <time.h>
using namespace std;
int main()
{
// welcome messgae
cout << "**************************************" << endl;
cout << "* 欢迎来到幸运号码游戏 *" << endl;
cout << "* 请输入你的 5 个幸运号码 *" << endl;
cout << "* 请用空格键分开 *" << endl;
cout << "**************************************" << endl;
cout << '\n';
// ask user to input
cout << "请输入你的5个幸运号码(1-70): ";
// create variables
int lucky5[5] = {};
int luckyresult;
int random5[5] = {};
int randomresult;
// create counters for loop cout usage
int i, j, y, z = 0;
// assigning user's input into the array
cin >> lucky5[0] >> lucky5[1] >> lucky5[2] >> lucky5[3] >> lucky5[4];
// User's input message
cout << "你的输入: ";
// display user's input from the array
for (luckyresult = 0; luckyresult <= 4; luckyresult++)
{
cout << lucky5[luckyresult] << ' ';
}
// print a new line for easier to read
cout << '\n';
// initialize random seed
srand(time(NULL));
// Random number section
for (i = 0; i <= 4; i++)
{
random5[i] = rand() % 30 + 1;
}
// random-number's message
cout << "开奖结果: ";
// display random-number's array
for (randomresult = 0; randomresult <= 4; randomresult++)
{
cout << random5[randomresult] << ' ';
}
cout << '\n';
// compare both arrays
for (j = 0; j <= 4; j++)
{
for (y = 0; y <= 4; y++)
{
if (lucky5[y] == random5[j])
{
z++;
}
}
}
// display the matched count
cout << "获奖结果: 你中了 " << z << " 个号码" << endl;
cout << '\n';
system("pause");
return 0;
}
结论:随机号码这东西,还是看缘分。
|