听峰问雨 听峰问雨
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)

zfprotectors

默默学习er
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)
  • UVA

    • UVA340 - Master-Mind Hints
      • 问题描述
      • 思路
      • 代码
    • UVA401 - Palindromes
    • UVA409 - Excuses, Excuses!
    • UVA414 - Machined Surfaces
    • UVA424 - Integer Inquiry
    • UVA445 - Marvelous Mazes
    • UVA457 - Linear Cellular Automata
    • UVA458 - The Decoder
    • UVA465 - Overflow
    • UVA488 - Triangle Wave
    • UVA489 - Hangman Judge
    • UVA490 - Rotating Sentences
    • UVA494 - Kindergarten Counting Game
    • UVA537 - Artificial Intelligence?
    • UVA644 - Immediate Decodability
    • UVA694 - The Collatz Sequence
    • UVA748 - Exponentiation
    • UVA10010 - Where's Waldorf?
    • UVA10055 - Hashmat the Brave Warrior
    • UVA10071 - Back to High School Physics
    • UVA10106 - Product
    • UVA10115 - Automatic Editing
    • UVA10250 - The Other Two Trees
    • UVA10300 - Ecological Premium
    • UVA10361 - Automatic Poetry
    • UVA10420 - List of Conquests
    • UVA10494 - If We Were a Child Again
    • UVA10815 - Andy's First Dictionary
    • UVA10878 - Decode the tape
  • LeetCode

  • ACM
  • UVA
zfprotectors
2022-05-18
目录

UVA340 - Master-Mind Hints

# 问题描述

猜数字,先给定四个数,然后再输入四个数,如果数字和位置一致,则a++,如果数字正确,则为b++,结果输出(a,b)

# 思路

学习了map以后,发现可以将各种数据类型联系起来,先记录正确数值,在开始判断数值且位置相等,则A++,然后计算总共猜对了B个数后结果为(A,B-A)

# 代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <vector>
#include <map>
using namespace std;
int main()
{
    int a[1010],b[1010];
    int n,i,t=1;
    int A,B;
    while(cin>>n&&n)
    {
        map<int,int> q;
        cout<<"Game "<<t++<<":\n";
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            q[a[i]]++;  //记录正确值
        }
        while(1)
        {
            map<int,int>p;
            A=B=0;
            for(i=0;i<n;i++)
            {
                cin>>b[i];
                p[b[i]]++;
                if(a[i]==b[i])      //如果位置和数值一致
                    A++;
            }
            if(b[0]==0)
                break;
            for(i=1;i<=9;i++)       //计算1—9之间总共猜对了B个数
                B+=min(p[i],q[i]);
            printf("    (%d,%d)\n",A,B-A);

        }
    }
    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
编辑 (opens new window)
#ACM#C++#UVA
上次更新: 2022/05/18, 19:03:08
UVA401 - Palindromes

UVA401 - Palindromes→

最近更新
01
LeetCode88 - 合并两个有序数组
06-22
02
LeetCode1 - 两数之和
06-22
03
LeetCode1603 - 设计停车系统
06-21
更多文章>
Theme by Vdoing | Copyright © 2021-2022 zfprotectors | 闽ICP备2021014222号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式