1-1
编写一个“抽签”的程序,生成0~6的随机数,根据值来显示“大吉”“中吉”“小吉”“吉”“末吉”“凶”“大凶”。
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
int ans = rand() % 7; //要生成0-6 则用rand()% 7
// rand() % (a + 1) //生成大于等于0 && 小于a 的随机整数
switch (ans)
{
case 0:
cout << "大吉" << endl;
break;
case 1:
cout << "中吉" << endl;
break;
case 2:
cout << "小吉" << endl;
break;
case 3:
cout << "吉" << endl;
break;
case 4:
cout << "末吉" << endl;
break;
case 5:
cout << "凶" << endl;
break;
case 6:
cout << "大凶" << endl;
break;
}
return 0;
}
1-2
把上一练习中的程序加以改良,使求出某些运势的概率与求出其他运势的概率不相等(例如可以把求出“末吉”“凶”“大凶”的概率减小)。
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
int randValue = rand() % 100; // 生成0到99之间的随机数
string fortune;
if (randValue < 20) { // 20%概率
fortune = "大吉";
} else if (randValue < 40) { // 20%概率
fortune = "中吉";
} else if (randValue < 60) { // 20%概率
fortune = "小吉";
} else if (randValue < 75) { // 15%概率
fortune = "吉";
} else if (randValue < 85) { // 10%概率
fortune = "末吉";
} else if (randValue < 95) { // 10%概率
fortune = "凶";
} else { // 5%概率
fortune = "大凶";
}
cout << fortune << endl;
return 0;
}
1-3
编写一个“猜数游戏”,让目标数字是一个在-999和999之间的整数。同时还需思考应该把玩家最多可输人的次数定在多少合适。
#include <iostream>
#include <ctime>
#define MAX_ATTEMPTS 10
using namespace std;
int main()
{
srand(time(NULL));
int ans = -999 + rand() % 1999; // 生成 -999 到 999 之间的随机数
int guess = 0, attempts = 0;
cout << "猜数游戏开始!目标数字在 -999 到 999 之间。" << endl;
cout << "你有 " << MAX_ATTEMPTS << " 次机会来猜这个数字。" << endl;
do
{
cout << "请输入你的猜测:";
cin >> guess;
attempts++;
if (guess < ans)
{
cout << "猜小了" << endl;
}
else if (guess > ans)
{
cout << "猜大了" << endl;
}
} while (guess != ans && attempts < MAX_ATTEMPTS);
if (guess == ans)
{
cout << "恭喜你,猜中了!用了 " << attempts << " 次。" << endl;
}
else
{
cout << "很遗憾,你已经用完了所有的机会。目标数字是 " << ans << "。" << endl;
}
return 0;
}
1-4
编写一个“猜数游戏”,让目标数字是一个在3和999之间的3的倍数(例如3,6,9,,999)。编写以下两种功能:一种是当输人的值不是3的倍数时,游戏立即结束;另一种是当输人的值不是3的倍数时,不显示目标数字和输人的数值的比较结果,直接让玩家再次输入新的数值(不作为输人次数计数)。
//功能一:当输入的值不是3的倍数时,游戏立即结束
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
int ans = 3 + (rand() % 333) * 3; // 生成3到999之间的3的倍数
int guess = 0, attempts = 0, maxAttempts = 10;
cout << "猜数游戏开始!目标数字是3到999之间的3的倍数。" << endl;
cout << "你有 " << maxAttempts << " 次机会来猜这个数字。" << endl;
while (attempts < maxAttempts)
{
cout << "请输入你的猜测:";
cin >> guess;
if (guess % 3 != 0)
{
cout << "输入的值不是3的倍数,游戏结束。" << endl;
break;
}
attempts++;
if (guess < ans)
{
cout << "猜小了" << endl;
}
else if (guess > ans)
{
cout << "猜大了" << endl;
}
else
{
cout << "恭喜你,猜中了!用了 " << attempts << " 次。" << endl;
break;
}
if (attempts == maxAttempts)
{
cout << "很遗憾,你已经用完了所有的机会。目标数字是 " << ans << "。" << endl;
}
}
return 0;
}
//功能二:当输入的值不是3的倍数时,直接让玩家再次输入新的数值(不作为输入次数计数)
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
int ans = 3 + (rand() % 333) * 3; // 生成3到999之间的3的倍数
int guess = 0, attempts = 0, maxAttempts = 10;
cout << "猜数游戏开始!目标数字是3到999之间的3的倍数。" << endl;
cout << "你有 " << maxAttempts << " 次机会来猜这个数字。" << endl;
while (attempts < maxAttempts)
{
cout << "请输入你的猜测:";
cin >> guess;
if (guess % 3 != 0)
{
cout << "输入的值不是3的倍数,请重新输入。" << endl;
continue; // 跳过当前循环,不增加尝试次数
}
attempts++;
if (guess < ans)
{
cout << "猜小了" << endl;
}
else if (guess > ans)
{
cout << "猜大了" << endl;
}
else
{
cout << "恭喜你,猜中了!用了 " << attempts << " 次。" << endl;
break;
}
if (attempts == maxAttempts)
{
cout << "很遗憾,你已经用完了所有的机会。目标数字是 " << ans << "。" << endl;
}
}
return 0;
}
1-5
编写一个“猜数游戏”,不事先决定目标数字的范围,而是在运行程序时才用随机数决定目标数字。打个比方,如果生成的两个随机数是23和8124,那么玩家就需要猜一个在23和8124之间的数字。
使用了int maxAttempts = log2(range) + 1;
对数函数计算尝试次数。
#include <iostream>
#include <ctime>
#include <cmath> // 用于计算尝试次数
using namespace std;
int main()
{
srand(time(NULL));
int ans1 = rand();
int ans2 = rand();
// 确保 ans1 > ans2
if (ans1 < ans2)
{
int tmp = ans1;
ans1 = ans2;
ans2 = tmp;
}
// 生成 ans2 和 ans1 之间的随机数
int ans = ans2 + rand() % (ans1 - ans2 + 1);
// 根据范围自动计算最多的尝试次数(例如,使用对数函数)
int range = ans1 - ans2 + 1;
int maxAttempts = log2(range) + 1; // 使用对数函数计算尝试次数
int attempts = 0;
int guess = 0;
cout << "猜测一个在 " << ans2 << " 和 " << ans1 << " 之间的数字。" << endl;
cout << "你有 " << maxAttempts << " 次机会。" << endl;
while (attempts < maxAttempts)
{
cout << "请输入你的猜测:";
cin >> guess;
attempts++;
if (guess < ans)
{
cout << "猜小了" << endl;
}
else if (guess > ans)
{
cout << "猜大了" << endl;
}
else
{
cout << "恭喜你,猜中了!用了 " << attempts << " 次。" << endl;
break;
}
if (attempts == maxAttempts)
{
cout << "很遗憾,你已经用完了所有的机会。目标数字是 " << ans << "。" << endl;
}
}
return 0;
}
1-6
#include <iostream>
#include <ctime>
#include <cmath> // 用于计算尝试次数
using namespace std;
int main()
{
srand(time(NULL));
int minRange = 1, maxRange = 9;
int choice;
cout << "请选择难度等级:" << endl;
cout << "(1) 1~9" << endl;
cout << "(2) 1~99" << endl;
cout << "(3) 1~999" << endl;
cout << "(4) 1~9999" << endl;
cin >> choice;
switch (choice)
{
case 1:
maxRange = 9;
break;
case 2:
maxRange = 99;
break;
case 3:
maxRange = 999;
break;
case 4:
maxRange = 9999;
break;
default:
cout << "无效的选择,默认为难度等级1(1~9)" << endl;
maxRange = 9;
}
int ans = minRange + rand() % (maxRange - minRange + 1);
// 根据范围自动计算最多的尝试次数(例如,使用对数函数)
int range = maxRange - minRange + 1;
int maxAttempts = log2(range) + 1; // 使用对数函数计算尝试次数
int attempts = 0;
int guess = 0;
cout << "猜测一个在 " << minRange << " 和 " << maxRange << " 之间的数字。" << endl;
cout << "你有 " << maxAttempts << " 次机会。" << endl;
while (attempts < maxAttempts)
{
cout << "请输入你的猜测:";
cin >> guess;
attempts++;
if (guess < ans)
{
cout << "猜小了" << endl;
}
else if (guess > ans)
{
cout << "猜大了" << endl;
}
else
{
cout << "恭喜你,猜中了!用了 " << attempts << " 次。" << endl;
break;
}
if (attempts == maxAttempts)
{
cout << "很遗憾,你已经用完了所有的机会。目标数字是 " << ans << "。" << endl;
}
}
return 0;
}
1-7
使用List1-8的程序时,即使玩家所猜数字和正确答案的差值是0,输人记录的显示结果也会带有符号,这样不太好看。请大家改进一下程序,让差值0不带符号。
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_STAGE 10 // 最多可以输入的次数
int main(void)
{
int i; // 计数器
int stage; // 已输入的次数
int no; // 读取的值
int ans; // 目标数字
int num[MAX_STAGE]; // 读取的值的历史记录
srand(time(NULL)); // 设定随机数的种子
ans = rand() % 1000; // 生成0~999的随机数
printf("请猜一个0~999的整数。\n\n");
stage = 0;
do
{
printf("还剩%d次机会。是多少呢:", MAX_STAGE - stage);
scanf("%d", &no);
num[stage++] = no; // 把读取的值存入数组
if (no > ans)
printf("\a再小一点。\n");
else if (no < ans)
printf("\a再大一点。\n");
} while (no != ans && stage < MAX_STAGE);
if (no != ans)
printf("\a很遗憾,正确答案是%d。\n", ans);
else
printf("回答正确。\n");
printf("您用了%d次猜中了。\n", stage);
puts("\n---输入记录---");
for (i = 0; i < stage; i++)
{
int diff = num[i] - ans;
if (diff == 0)
printf("%2d: %4d\n", i + 1, num[i]); // 差值为0时不显示符号
else
printf("%2d: %4d %+4d\n", i + 1, num[i], diff); // 显示差值并带符号
}
return 0;
}
1-8
把List1-8里的do语句改写成for语句。
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_STAGE 10 // 最多可以输入的次数
int main(void)
{
int i; // 计数器
int stage; // 已输入的次数
int no; // 读取的值
int ans; // 目标数字
int num[MAX_STAGE]; // 读取的值的历史记录
srand(time(NULL)); // 设定随机数的种子
ans = rand() % 1000; // 生成0~999的随机数
printf("请猜一个0~999的整数。\n\n");
for (stage = 0; stage < MAX_STAGE; stage++)
{
printf("还剩%d次机会。是多少呢:", MAX_STAGE - stage);
scanf("%d", &no);
num[stage] = no; // 把读取的值存入数组
if (no > ans)
printf("\a再小一点。\n");
else if (no < ans)
printf("\a再大一点。\n");
else
{
printf("回答正确。\n");
break;
}
}
if (stage == MAX_STAGE)
printf("\a很遗憾,正确答案是%d。\n", ans);
if(stage != MAX_STAGE)
printf("您用了%d次猜中了。\n", stage + 1);
puts("\n---输入记录---");
for (i = 0; i <= stage; i++) // 修改循环条件为 <= stage
{
if (i + 1 == 11) break;
int diff = num[i] - ans;
if (diff == 0)
printf("%2d: %4d %+4d\n", i + 1, num[i],0); // 差值为0时不显示符号
else
printf("%2d: %4d %+4d\n", i + 1, num[i], diff); // 显示差值并带符号
}
return 0;
}