算法竞赛入门经典第四章

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// t4.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
//素数判断方法2
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<string.h>
#define Max 1000000
#define maxn 100
int left, chance; //还需要left位置,错chance之后就会输
char s[maxn], s2[maxn]; //答案是字符串s, 玩家猜的字母序列是s2
int win, lose; // win=1 表示已经赢了; lose=1 表示已经输了

long fac(int n) 
{
	if (0 == n || 1 == n)
	{
		return 1;
	}
	else
	{
		return fac(n - 1)*n;
	}
}

// 非递归的阶乘方法 
long fact(int n)
{
	long iRes = 1;
	for (int i = 1; i <= n; i++)
	{
		iRes *= i;
	}
	return iRes;
}
int combination(int m, int n)
{
	long iRes = fac(n) / (fac(m)*fac(n - m));

	// long iRes = fact(n) /(fac(m)*fac(n-m));

	printf_s("%ld %.2lf \n", iRes, (double)clock() / CLOCKS_PER_SEC);
	return iRes;
}
// 刽子手游戏---guess函数
void guess(char ch)
{
	int bad = 1;
	for (int i = 0; i < strlen(s); i++)
		if (s[i] == ch) { left--; s[i] = ' '; bad = 0; }
	if (bad) --chance;
	if (!chance) lose = 1;
	if (!left) win = 1;
}
int main()
{

	/*
	关键:
	1 用素数筛选法先预处理,默认刚开始全为素数,然后对素数的倍数标记为非素数,			for(int j = i*i ; j <= 10000 ; j += i){iPrimeArr[j] = 1;}
	2 通过开根号判断素数时,可以用floor,注意浮点数加上0.5,int iRadical = floor(sqrt(n*1.0) + 0.5);
	3 可以用assert()对输入的合法性进行校验,assert(n >= 5 && n <= 10000);void assert(int exp),如果表达式的值为0则退出。*/

	//int sum = 1;
	//for (int i = 3; i <= Max; i += 2)
	//{
	//	//因为偶数除了2 都不是质数
	//	int j;
	//	for (j = 2; j <= (int)sqrt(i); j++)//利用上述结论判断
	//		if (i%j == 0) break;
	//	if (j > (int)sqrt(i))
	//		sum++;
	//}
	//printf_s("Time used = %0.2f s\n", (double)clock() / CLOCKS_PER_SEC);
	//printf_s("%d\n", sum);

	//int a, b;
	//scanf_s("%d %d", &a, &b);
	//combination(a, b);

	//刽子手游戏
	/*
	游戏规则是这样的:计算机想一个单词让你猜,你每次可以猜一个字母。 如果单词里有那个字母,所有该字母会显示出来;如果没有那个字母,则计算机会在一幅“刽子手”画上填一笔。 这幅画一共需要7笔就能完成,因此你最多只能错6次。 注意,猜一个已经猜过的字母也算错。
	在本题中,你的任务是编写一个“裁判”程序,输入单词和玩家的猜测,判断玩家赢了(You win.)、 输了(You lose.)还是放弃了(You chickened out.)。 每组数据包含3行,第1行是游戏编号(-1为输入结束标记),第2行是计算机想的单词,第3行是玩家的猜测。 后两行保证只含小写字母。
	*/
	//char ans[21];
	//char gus[28];
	//int times, yes;
	//int chances = 7;
	//int win = 0;
	//int lose = 0;
	//scanf_s("%d", &times);
	//while (times != -1)
	//{
	//	printf_s("Round %d\n", times);
	//	scanf_s("%s\n %s", ans, gus);
	//	printf_s("Round %s &s\n", ans, gus);
	//	yes = 0;
	//	win = 0, lose = 0, chances = 7;
	//	for (int i = 0; i < strlen(gus); i++) 
	//	{
	//		int flag = 0;
	//		for (int j = 0; j < strlen(ans); j++)
	//		{
	//			if (ans[j] == gus[i])
	//			{
	//				yes++;
	//				flag = 1;//找到之后不退出,因为有一个有相同的字母
	//			}
	//			if (flag == 0)
	//			{
	//				chances--;
	//			}
	//			if (chances == 0)
	//			{
	//				lose = 1;
	//				printf_s("You lose.\n");

	//			}
	//			else if (yes == strlen(ans))
	//			{
	//				win = 1;
	//				printf_s("You win.\n");
	//				break;
	//			}
	//			if (win != 1 && lose != 1)
	//			{
	//				printf("You chickened out.\n");
	//			}
	//			scanf_s("%d", &times);
	//		}
	//	}
	//}
	int rnd;
	while (scanf_s("%d%s%s", &rnd, &s, &s2) == 3 && rnd != -1)
	{
		printf_s("%Round %d\n", rnd);
		win = lose = 0;
		left = strlen(s);
		chance = 7;
		for (int i = 0; i < strlen(s2); i++)
		{
			guess(s2[i]); //猜一个字母
			if (win || lose) break; //检查状态

		}
		//根据结果进行输出
		if (win) printf_s("You win.\n");
		else if (lose) printf_s("You lose.\n");
		else printf_s("You chickened out.\n");
	}
	system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值