UVA-340 Master-Mind Hints

来源:互联网 发布:python scope visiual 编辑:程序博客网 时间:2024/06/05 05:09

2016-07-16

UVA - 340 Master-Mind Hints

题目大意:几 A 几 B 问题。第一行为密码。接下来输入的几行分别与密码相比,求位置相同且数字相同的有几个,数字相同而位置不同的有几个,每个数字只能用一次。

解题思路:读入密码和猜测的数字,一一比较就可以,相等的话就令两个位置分别为字符a和b,注意保留密码。

注意:用字符串直接将密码全部保存比较方便。每个数字匹配过就不能再用了。数组要开大点,否则容易RE。

#include <iostream>#include <cstdio>#include <cstring>#include <stdlib.h>using namespace std;int num = 0;//统计有多少个游戏,方便输出。char c;char s[10000];char str[10000][10000];int ans[10000][2];char flag[10000] ;int main() {    int n;    while ( cin >> n && n != 0 ) {        int x = 0 ,y = 0;        memset( ans , 0 , sizeof(ans) );        getchar();        while ( scanf("%c",&c) && c != '0') {            if ( x == n ) {                y++;                x = 0;            }            if ( c >= '1' && c <= '9' ) {                str[y][x] = c;                x++;            }        }        gets(s);        int temp;        strcpy(flag,str[0]);        for (int i = 1; i < y; i++) {            for (int j = 0; j < n; j++) {                                if ( str[0][j] == str[i][j] ) {                                                       ans[i-1][0]++;                                        str[0][j] = 'a';                                        str[i][j] = 'b';                                }                        }            temp = 0;            while ( temp < n ) {                for (int j = 0; j < n; j++) {                    if ( str[0][temp] == str[i][j] ) {                            ans[i-1][1]++;                        str[0][temp] = 'a';                        str[i][j] = 'b';                    }                }                temp++;            }            strcpy(str[0],flag);        }        num++;        cout << "Game " << num << ":" << endl;        for (int i = 0; i < y-1; i++)            cout << "    (" << ans[i][0] << "," << ans[i][1] << ")" << endl;            }    return 0;}


0 0
原创粉丝点击