《算法竞赛入门经典2ndEdition》 例题3-4 猜数字游戏的提示(Master-Mind Hints, Uva340)
来源:互联网 发布:编程项目 编辑:程序博客网 时间:2024/04/20 01:31
看了好久还是没看懂题,看了这位大哥(http://www.cppblog.com/rakerichard/archive/2011/04/09/143775.html)翻译的再去看了下题才理解了题意,那个strong weak 串第一开始没读懂,结果只是按照样例弄的,现在终于AC了。 英语不好是硬伤。。。。
#include <cstdio>#include <cstring>#include <algorithm>const int maxn = 1000;int num[maxn + 20], book[10], booknum[10], guess[maxn + 20], bookguess[10];using namespace std;int main(){ //freopen("New Text Document.txt","r",stdin); //freopen("Output.txt","w",stdout); int n, game = 0; while(scanf("%d", &n) == 1 && n) { printf("Game %d:\n",++game); memset(book, 0, sizeof(book)); for(int i = 1; i <= n; i++) { scanf("%d", &num[i]); book[num[i]]++; } int x; while(scanf("%d", &x) && x) { memset(bookguess, 0, sizeof(bookguess)); for(int i = 1; i<= 9; i++) booknum[i] = book[i]; int right = 0, appear = 0; guess[1] = x; bookguess[x]++; if(guess[1] == num[1]) { right++; booknum[x]--; bookguess[x]--; } for(int i = 2; i <= n; i++) { scanf("%d", &guess[i]); bookguess[guess[i]]++; if(guess[i] == num[i]) { right++; booknum[guess[i]]--; bookguess[guess[i]]--; } } for(int i = 1; i <= 9; i++) { if(bookguess[i] && booknum[i]) appear += min(bookguess[i], booknum[i]); } printf(" (%d,%d)\n",right,appear); } for(int i = 2; i <= n; i++) scanf("%d", &x); } //getchar();getchar(); return 0;}
底下是刘汝佳标程
https://github.com/aoapc-book/aoapc-bac2nd/blob/master/ch3/UVa340.cpp
// UVa340 Master-Mind Hints// Rujia Liu#include<stdio.h>#define maxn 1000 + 10int main() { int n, a[maxn], b[maxn]; int kase = 0; while(scanf("%d", &n) == 1 && n) { // n=0时输入结束 printf("Game %d:\n", ++kase); for(int i = 0; i < n; i++) scanf("%d", &a[i]); for(;;) { int A = 0, B = 0; for(int i = 0; i < n; i++) { scanf("%d", &b[i]); if(a[i] == b[i]) A++; } if(b[0] == 0) break; // 正常的猜测序列不会有0,所以只判断第一个数是否为0即可 for(int d = 1; d <= 9; d++) { int c1 = 0, c2 = 0; // 统计数字d在答案序列和猜测序列中各出现多少次 for(int i = 0; i < n; i++) { if(a[i] == d) c1++; if(b[i] == d) c2++; } if(c1 < c2) B += c1; else B += c2; } printf(" (%d,%d)\n", A, B-A); } } return 0;}
1 0
- 《算法竞赛入门经典2ndEdition》 例题3-4 猜数字游戏的提示(Master-Mind Hints, Uva340)
- 例题3-4 猜数字游戏的提示(Master-Mind Hints)
- 算法竞赛入门经典 例题3-4 猜数字游戏的提示
- UVa 340 Master-Mind Hints(猜数字游戏的提示)
- 猜数字游戏的提示 (Master-Mind Hints, UVa 340)
- 例题3-4 猜数字游戏的提示 UVa340
- uva340 - Master-Mind Hints
- UVa340 Master-Mind Hints
- UVA340 - Master-Mind Hints
- UVA340 Master-Mind Hints
- uva340 - Master-Mind Hints
- UVA340 - Master-Mind Hints
- UVa340 Master-Mind Hints
- uva340 Master-Mind Hints
- UVa340-Master-Mind Hints
- UVa340 Master-Mind Hints
- UVa 340 猜数字游戏提示(Master-Mind-Hints)
- UVA 340 Master-Mind Hints 猜数字提示的游戏 简单模拟
- R机器学习之交叉验证改善模型
- android开发中,通过adb抓取log信息
- 广搜1—— Meteor Shower(流星雨)
- Android自定义View动画(进度条)
- Ubuntu下mysql解决中文乱码
- 《算法竞赛入门经典2ndEdition》 例题3-4 猜数字游戏的提示(Master-Mind Hints, Uva340)
- 何为大数据?
- 使用 Spring、Elasticsearch 及 Logstash 构建企业级数据搜索和分析平台
- 使用除数时有小数点
- iOS 保持界面流畅的技巧
- View ViewGroup 关于setTag 和getTag的应用 findViewWithTag
- 多事件引擎实现
- 设置 cell 的背景色,背景图 ,选中效果
- 给mysql用户重新设置权限后报错