Files
python/RuMenJingDian/LiTi/P_3_4.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

34 lines
1.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
#define maxn 1010
int main(void) {
int n, i, j, k = 0;
int a[maxn], b[maxn];
while (scanf("%d", &n) == 1 && n)//n=0时输入结束
{
printf("Game %d:\n", ++k);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
while (1)//可改为for(;;)
{
int A = 0, B = 0, count = 0;
for (i = 0; i < n; i++) {
scanf("%d", &b[i]);
if (a[i] == b[i]) A++;
if (b[i] == 0) count++;//统计输入0的个数
}
if (count == n) break;//如果四位数皆为零,则输入结束
for (j = 0; j <= 9; j++) {
int a1 = 0, a2 = 0;//统计数字j在答案序列和猜测序列中各出现多少次数
for (i = 0; i < n; i++) {
if (a[i] == j) a1++;//统计a[i]、b[i]中j的个数
if (b[i] == j) a2++;
}
if (a1 < a2) B = B + a1;//记统计的最小的个数 即为序列拥有相同元素的个数
else B = B + a2;
}
printf(" (%d,%d)\n", A, B - A);//B减去相同位置的个数A即为元素相同但位置不同的个数
}
}
}