zoukankan      html  css  js  c++  java
  • 猜数字游戏的提示

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

    题目描述:

     

     

    我的代码:

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

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

     

  • 相关阅读:
    matab plot指令和低通滤波器的响应图
    matlab中hold指令、figure指令及subplot指令的使用
    matlab中axis的使用
    matlab switch case 和 try catch用法示例
    matlab中使用elseif和if嵌套的对比
    matlab中运用项目思维分析问题并解决问题
    matlab图像显示程序模板
    matlab最简单程序模板
    第06篇 MyEclipse 2016 安装/破解
    第05篇. Tomcat和JDK的内存配置
  • 原文地址:https://www.cnblogs.com/Breathmint/p/7226023.html
Copyright © 2011-2022 走看看