练习1-1
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void)
{
srand(time(NULL));
int i = rand() % 7;
printf("%d:", i);
if (i == 0)
printf("大吉\n");
else if (i == 1)
printf("中吉\n");
else if (i == 2)
printf("小吉\n");
else if (i == 3)
printf("吉\n");
else if (i == 4)
printf("末吉\n");
else if (i == 5)
printf("凶\n");
else
printf("大凶\n");
return 0;
}
练习1-2
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void)
{
srand(time(NULL));
int i = rand() % 11; //生成0~10这11个随机数,令0、1为大吉,2、3为中吉,4、5为小吉,6、7为吉;8为末吉,9为凶,10为大凶
printf("%d:", i);
if (i == 0||i==1)
printf("大吉\n");
else if (i == 2||i==3)
printf("中吉\n");
else if (i == 4||i==5)
printf("小吉\n");
else if (i == 6||i==7)
printf("吉\n");
else if (i == 8)
printf("末吉\n");
else if (i == 9)
printf("凶\n");
else
printf("大凶\n");
return 0;
}
练习1-3
将玩家的输入次数定在11次较为合适,2^11=2048,根据二分法,11次机会有较大概率猜出数字。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAX_STAGE 11
int main(void)
{
srand(time(NULL));
int i, j;
int num;
int stage = 0;
int a[MAX_STAGE];
i = rand() % 1999 - 999;
printf("请输入一个介于-999~999之间的数。\n");
do {
printf("还剩%d次机会,是多少呢:",MAX_STAGE-stage);
scanf("%d", &num);
a[stage++] = num;
if (num < i)
printf("再大一点。\n\n");
else if (num > i)
printf("再小一点。\n\n");
} while (num != i || stage < MAX_STAGE);
if (num != i)
printf("\a很遗憾,正确答案是%d。\n\n",i );
else
printf("恭喜你在第%d次猜对了,这个数是%d\n\n", stage, i);
puts("\n---输入记录---\n");
for (j = 0; j < stage; j++)
printf("第%2d次:%4d %+4d\n", j + 1, a[j], a[j] - num);
return 0;
}
练习1-4
本题使用了goto函数,在(1)模式中,当输入的数值不是3的倍数,会直接结束程序。
本题将玩家的输入次数定在9次,2^9=512>333,根据二分法,9次机会有较大概率猜出数字;
当设置8次机会时,正确猜出数字的可能性会减小。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAX_STAGE 9
int main(void)
{
int ans;
int i;
int num;
int model;
int stage = 0;
int a[MAX_STAGE];
srand(time(NULL));
ans = 3 * (1 + rand() % 333);
do {
printf("请选择模式---当输入的值不是3的倍数时(1)直接结束(2)重新输入:");
scanf("%d", &model);
} while (model < 1 || model>2);
if (model == 1) {
do {
printf("还剩%d次机会,请输入3~999之间的3的倍数:",MAX_STAGE-stage);
scanf("%d", &num);
a[stage++] = num;
if ((num % 3) != 0)
goto exit_all_loop;
else if (num > ans)
printf("再小一点。\n");
else if (num < ans)
printf("再大一点。\n");
} while (num != ans && stage < MAX_STAGE);
if (num != ans)
printf("很遗憾回答错误,正确答案是%d。\n", ans);
else if (num == ans) {
printf("恭喜你回答正确,你共用了%d次机会。\n", stage);
puts("\n---输入记录---\n");
for (i = 0; i < stage; i++)
printf("%2d:%4d %+4d\n", i + 1, a[i], a[i] - ans);
}
}
else if (model == 2) {
do {
printf("还剩%d次机会,请输入3~999之间的3的倍数:", MAX_STAGE - stage);
scanf("%d", &num);
if ((num % 3) != 0);
else if (num > ans) {
a[stage++] = num;
printf("再小一点。\n");
}
else if (num < ans) {
a[stage++] = num;
printf("再大一点。\n");
}
} while (num != ans && stage < MAX_STAGE);
if (num != ans)
printf("很遗憾回答错误,正确答案是%d。\n", ans);
else if (num == ans) {
a[stage++] = num;
printf("恭喜你回答正确,你共用了%d次机会。\n", stage);
for (i = 0; i < stage; i++)
printf("%2d:%4d %+4d\n", i + 1, a[i], a[i] - ans);
}
}
exit_all_loop:
return 0;
}
练习1-5
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main(void)
{
int ans;
int ran1,ran2;
int i, j;
int stage, num;
int a, b;
srand(time(NULL));
ran1 = rand();
ran2 = rand();
ans = abs(ran1 - ran2);
for (i = 0; i < 32; i++) //编译环境中int占据4个字节,即32比特
if ((pow(2, i) < ans) && (pow(2, i + 1) >= ans))
{
stage = i;
num = i;
break;
}
if (ran1 < ran2) {
a = ran1;
b = ran2;
}
else {
a = ran2;
b = ran1;
}
printf("请输入一个正整数。\n");
do {
printf("你还有%d次机会,是多少呢:",stage);
scanf("%d", &j);
stage--;
if (a > j)
printf("再大一点。\n");
else if(b < j)
printf("再小一点。\n");
} while ((a > j) || (b < j) && stage > 0);
if ((a < j) && (b > j))
printf("恭喜你回答正确,生成的随机数为%d和%d,你共使用了%d次机会。\n", a, b, num - stage);
else
printf("很遗憾没有猜对,生成的随机数为%d和%d。\n", a, b);
return 0;
}
练习1-6
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main(void)
{
int i, j;
int ans;
int model;
int stage;
int num;
do {
printf("请选择模式---(1)模式一、(2)模式二、(3)模式三、(4)模式四:");
scanf("%d", &model);
} while (model < 1 || model>4);
j = pow(10, model) - 1;
stage = 3 * model + 1; //模式一有4次机会,模式二有7次机会,模式三有10次机会,模式四有13次机会
srand(time(NULL));
ans = 1 + rand() % j;
printf("请输入%d~%d之间的数字:", 1, j);
do {
printf("你还有%d次机会,是多少呢:",stage);
scanf("%d", &num);
stage--;
if (num < ans)
printf("再大一点。\n");
else if (num > ans)
printf("再小一点。\n");
} while (num != ans && stage > 0);
if (num != ans)
printf("很遗憾回答错误,正确答案是%d。\n", ans);
else
printf("恭喜你在第%d次回答正确。\n", 3 * model + 1 - stage);
return 0;
}
练习1-7
#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;
printf("请输入一个0~999的整数。\n\n");
stage = 0;
do {
printf("还剩%d次机会,是多少呢:",MAX_STAGE-stage);
scanf("%d", &no);
num[stage++] = no;
if (no > ans)
printf("再小一点。\n");
else if (no < ans)
printf("再大一点。\n");
} while (no != ans && stage < MAX_STAGE);
if (no != ans)
printf("很遗憾,这确答案是%d。\n", ans);
else
printf("回答正确,你使用了%d次机会。\n",stage);
puts("\n---输入记录---\n");
for (i = 0; i < stage; i++)
printf("%2d:%4d %-4d\n", i + 1, num[i], num[i] - ans); //第三项数字输出改为:%-4d
return 0;
}
练习1-8
#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;
printf("请输入一个0~999的整数。\n\n");
stage = 0;
for(i=0;i<MAX_STAGE;i++){
printf("还剩%d次机会,是多少呢:",MAX_STAGE-stage);
scanf("%d", &no);
num[stage++] = no;
if (no > ans)
printf("再小一点。\n");
else if (no < ans)
printf("再大一点。\n");
else
break;
}
if (no != ans)
printf("很遗憾,这确答案是%d。\n", ans);
else
printf("回答正确,你使用了%d次机会。\n",stage);
puts("\n---输入记录---\n");
for (i = 0; i < stage; i++)
printf("%2d:%4d %-4d\n", i + 1, num[i], num[i] - ans);
return 0;
}