猜数字游戏的提示

题目来自刘汝佳编著的《算法竞赛入门经典(第二版)》

题目描述:

我的代码:

#include<iostream>
#include<cstring>
using namespace std;
int main() {
    int answer[100];
    int copy[100];
    int enter[100];
    int n, a, b;
    int count = 0;
    n = 1;
    while (n != 0)
    {
        cin >> n;

        for (int i = 0; i < n; i++)
            cin >> answer[i];
        cout << "Game " << ++count << endl;
        while (1)
        {

            a = b = 0;
            for (int i = 0; i < n; i++)
                copy[i] = answer[i];

            for (int i = 0; i < n; i++)
                cin >> enter[i];

            if (enter[0] == 0)
                break;

            for (int i = 0; i < n; i++)
            {
                if (copy[i] == enter[i])
                {
                    a++;
                    enter[i] = copy[i] = 0;
                }
            }

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (copy[i] == enter[j] && i != j && enter[j] != 0)
                    {
                        b++;
                        enter[j] = copy[i] = 0;
                    }
                }
            }
            cout << "\t" << "(" << a << "," << b << ")" << endl;
        }

    }
    return 0;
}

答案的代码:

#include<stdio.h>
#define maxn 1010

int main() {
    int n, a[maxn], b[maxn];
    int kase = 0;
    while (scanf("%d", &n) == 1 && n) {
        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;
            for (int d = 1; d <= 9; d++) {
                int c1 = 0, c2 = 0;
                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;
}

这次博主有点懒。。。题目就截图贴上去了,博主做完本题之后感觉。。。自己的算法不是很精妙,答案的思路很清奇,有没有大神能贡献自己的算法呢??╮(╯▽╰)╭

时间: 2024-08-05 06:41:01

猜数字游戏的提示的相关文章

猜数字游戏的提示(Master-Mind Hints,UVa340)

[本博文非博主原创,思路与题目均摘自 刘汝佳<算法竞赛与入门经典(第2版)>] Question 例题3-4 猜数字游戏的提示(Master-Mind Hints,UVa340) 实现一个经典的"猜数字"游戏.给定答案序列和用户猜的序列,统计有多少数字位置正确(A),有多少数字在两个序列都出现过但位置不对(B). 输入包含多组数据.每组输入第一行为序列长度 n,第二行是答案序列,接下来若干行猜测序列.猜测序列全0 时该组数据结束. n=0时输入结束. Example Inp

UVa 340 Master-Mind Hints(猜数字游戏的提示)

题意  猜数字游戏  统计猜的数字有多少个数字位置正确  有多少个数字在答案中出现但是位置不正确  每个字符只能匹配一次 直接匹配每位数 #include<cstdio> #include<algorithm> #include<map> using namespace std; const int N = 1005; int a[N], b[N], c, d; int main() { int n, cas = 0; while (scanf ("%d&qu

例题3_4 猜数字游戏的提示(UVa340)

实现一个经典“猜数字”游戏.给定答案序列和用户猜的序列,统计有多少数字位置正确(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

猜数字游戏的提示 (Master-Mind Hints, UVa 340)

题目: 实现一个经典"猜数字"游戏. 给定答案序列和用户猜的序列,统计有多少数字位置正确(A),有多少数字在两个序列都出现过但位置不对(B). 输入包含多组数据. 每组输入第一行为序列长度n,第二行是答案序列,接下来是若干猜测序列. 猜测序列全0时该组数据结束. n=0时输入结束. 样例输入:4 13 5 51 1 2 34 3 3 56 5 5 16 1 3 51 3 5 50 0 0 0101 2 2 2 4 5 6 6 6 91 2 3 4 5 6 7 8 9 11 1 2 2

猜数字游戏的提示(Master-Mind Hints , UVa 340)

MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players agree upon the length N that a code must

编程成长日记————猜数字游戏

模拟猜数字游戏,提示用户每次应输入的数字范围. #include <stdio.h> #include <stdlib.h> //猜数字游戏 int main() { int i=0; int num =0,ret=0,choose=0; int min=1,max=100; flag: srand((unsigned)time(NULL));  ret=rand()%100+1; while(1) { printf("请输入%d~%d之间的数字:\n",min

算法习题---3.01猜数字游戏提示

一.题目 实现一个经典“猜数字”游戏.给定答案序列和用户猜的序列,统计有多少数字位置正确(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 //

JavaScript一个猜数字游戏

效果图: 代码: <body> <script type="text/javascript"> window.onload = newgame; //页面载入的时候就开始一个新的游戏 window.onpopstate = popState; //处理历史记录相关事件 var state,ui; //全局变量,在newgame()方法中会对其初始化 function newgame( playagin ){ //开始一个新的猜数字游戏 //初始化一个包含需要的文

Python实现简单的猜数字游戏

Python实现简单的猜数字游戏,具体如下: 随机生成一个1-10之间的数字,让用户来猜,当猜错时,会提示猜的数字是大还是小了,直到用户猜对为止. import random secret = random.randint(1,10) #print(secret) print('------猜数字游戏!-----') guess = 0 while guess != secret: temp = input('猜数字游戏开始,请输入数字:') guess = int(temp) if guess