• 猜数字游戏的提示


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

    题目描述:

     

     

    我的代码:

    #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 << "	" << "(" << 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:
    ", ++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)
    ", A, B - A);
            }
        }
        return 0;
    }

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

     

  • 相关阅读:
    微信打开网址添加在浏览器中打开提示遮罩
    Java内存分配之堆、栈和常量池
    腾讯面试题04.进程和线程的区别?
    cookie 和 session的区别
    jvm内存模型-回收算法-和内存分配以及jdk、jre、jvm是什么关系(阿里,美团,京东面试题)
    HTTP中GET与POST的区别
    Socket send函数和recv函数详解
    JSP九大内置对象及四个作用域
    JavaScript
    网页布局(html+css基础)
  • 原文地址:https://www.cnblogs.com/Breathmint/p/7226023.html
Copyright © 2020-2023  润新知