猜数字游戏的提示 (Master-Mind Hints, UVa 340)
2017-01-14 13:42
417 查看
实现一个经典”猜数字”游戏。
给定答案序列和用户猜的序列,统计有多少数字位置正确(A),有多少数字在两个序列都出现过但位置不对(B)。
输入包含多组数据。每组输入第一行为序列长度n,第二行是答案序列,接下来是若干猜测序列。猜测序列全0时该组数据结束。n=0时输入结束。
样例输入:
4
1 3 5 5
1 1 2 3
4 3 3 5
6 5 5 1
6 1 3 5
1 3 5 5
0 0 0 0
10
1 2 2 2 4 5 6 6 6 9
1 2 3 4 5 6 7 8 9 1
1 1 2 2 3 3 4 4 5 5
1 2 1 3 1 5 1 6 1 9
1 2 2 5 5 5 6 6 6 7
0 0 0 0 0 0 0 0 0 0
0
样例输出:
Game 1:
(1,1)
(2,0)
(1,2)
(1,2)
(4,0)
Game 2:
(2,4)
(3,2)
(5,0)
(7,0)
输入及运行结果如下:
给定答案序列和用户猜的序列,统计有多少数字位置正确(A),有多少数字在两个序列都出现过但位置不对(B)。
输入包含多组数据。每组输入第一行为序列长度n,第二行是答案序列,接下来是若干猜测序列。猜测序列全0时该组数据结束。n=0时输入结束。
样例输入:
4
1 3 5 5
1 1 2 3
4 3 3 5
6 5 5 1
6 1 3 5
1 3 5 5
0 0 0 0
10
1 2 2 2 4 5 6 6 6 9
1 2 3 4 5 6 7 8 9 1
1 1 2 2 3 3 4 4 5 5
1 2 1 3 1 5 1 6 1 9
1 2 2 5 5 5 6 6 6 7
0 0 0 0 0 0 0 0 0 0
0
样例输出:
Game 1:
(1,1)
(2,0)
(1,2)
(1,2)
(4,0)
Game 2:
(2,4)
(3,2)
(5,0)
(7,0)
个人感觉此种题目训练标准输入格式时比较有用,练手用非常适合,以下贴出代码
#include <iostream> #include <stdio.h> #define max 1010 using namespace std; int main() { int num[max], guess[max]; int kase = 0; int n; while (scanf("%d", &n) == 1 && n != 0) { cout << "Game " << ++kase << endl; for (int j = 0; j < n; j++) { cin >> num[j]; } while (true) { int A = 0, B = 0; for (int j = 0; j < n; j++) { cin >> guess[j]; if (num[j] == guess[j]) { A++; } } if (guess[0] == 0) { break; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (num[i] == guess[j]) { B++; break; } } } printf(" (%d, %d)\n", A, B-A); } } return 0; }
输入及运行结果如下:
3 Game 1 1 2 3 1 2 3 (3, 0) 2 1 3 (1, 2) 0 0 0 4 Game 2 1 2 3 4 3 2 14 3 (1, 1) 0 0 0 0 0
相关文章推荐
- UVA 340 Master-Mind Hints 猜数字提示的游戏 简单模拟
- UVa 340 Master-Mind Hints(猜数字游戏的提示)
- UVa 340 Master-Mind Hints(猜数字游戏的提示)
- uva 340 -----Master-Mind Hints(猜数字游戏的提示)
- 3-4 猜数字游戏的提示(Master-Mind Hints, UVa 340)
- UVa 340 猜数字游戏提示(Master-Mind-Hints)
- UVa 340 Master-Minds Hints(猜数字游戏的提示)
- 猜数字游戏的提示( Master-Mind Hints UVA - 340)
- 猜数字游戏的提示(Master-Mind Hints, UVa 340)
- UVa 340 猜数字游戏的提示
- 例题3-4 猜数字游戏的提示 UVa340
- UVa 340 猜数字游戏的提示
- UVA340-猜数字游戏的提示
- UVA 340 Master-Mind Hints 猜密码游戏(水)
- 猜数字游戏的提示(Master-Mind Hints, UVa 340)
- 例题3_4 猜数字游戏的提示(UVa340)
- 猜数字游戏的提示(Master-Mind Hints, UVa 340)
- 《算法竞赛入门经典2ndEdition》 例题3-4 猜数字游戏的提示(Master-Mind Hints, Uva340)
- UVa340 - Master-Mind Hints
- UVa340 Master-Mind Hints