本帖最后由 hkivanleeon68 于 2018-10-9 12:55 编辑
学习写的 分享给大家 欢迎讨论
喜欢的可以拿回去自己生成玩一下
日后会慢慢根据学习更新
第一个版本传送门:
传送
2018-10-8-更新
改进了代码格式,加入了"特码"部分
[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';
// create variables
int lucky5[5] = {}; // array for putting user's first 5 digits input
int luckyresult; // get & display user's number result
int random5[5] = {}; // empty array for random number
int randomresult; // get & display randome number's result
int luckynumber; // hold 1 lucky number
int rluckynumber; // hold 1 random lucky number
// create counters for loop cout usage
int i, j, y, z = 0;
// ask user to input
cout << "请输入你的5个平码(1-70): ";
// assigning user's input into the array
cin >> lucky5[0] >> lucky5[1] >> lucky5[2] >> lucky5[3] >> lucky5[4];
// ask user to input lucky number
cout << "请输入你的特码(1-26): ";
// assignming user's input
cin >> luckynumber;
// User's input message
cout << '\n';
cout << "你的号码: " << endl;
cout << "平码: ";
// display user's input from the array
for (luckyresult = 0; luckyresult <= 4; luckyresult++)
{
cout << lucky5[luckyresult] << ' ';
}
// display user's input
cout << "特码: " << luckynumber;
// 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() % 70 + 1;
}
// random lucky number
rluckynumber = rand() % 26 + 1;
// random-number's message
cout << '\n';
cout << "开奖结果: " << endl;
cout << "平码: ";
// display random-number's array
for (randomresult = 0; randomresult <= 4; randomresult++)
{
cout << random5[randomresult] << ' ';
}
// display random lucky number
cout << "特码: " << rluckynumber;
cout << '\n';
// compare both arrays
for (j = 0; j <= 4; j++)
{
for (y = 0; y <= 4; y++)
{
if (lucky5[y] == random5[j])
{
z++;
}
}
}
// compare both number
if (luckynumber == rluckynumber)
{
z++;
}
// display the matched count
cout << '\n';
cout << "获奖结果: 你中了 " << z << " 个号码" << endl;
cout << '\n';
system("pause");
return 0;
}
结论:随机号码这东西,还是看缘分。
大致结果如下:
|