码迷,mamicode.com
首页 > 其他好文 > 详细

猜数字游戏的提示

时间:2017-07-23 22:52:14      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:==   思路   out   ace   ima   答案   自己   技术   can   

 

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

题目描述:
技术分享

 

 

技术分享

技术分享

 

 技术分享

技术分享

技术分享

我的代码:

#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;
}

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

 

猜数字游戏的提示

标签:==   思路   out   ace   ima   答案   自己   技术   can   

原文地址:http://www.cnblogs.com/Breathmint/p/7226023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!