本文共 3595 字,大约阅读时间需要 11 分钟。
Input
The input will consist of data for a number of games. The input for each game begins with an integer specifying N (the length of the code). Following these will be the secret code, represented as N integers, which we will limit to the range 1 to 9. There will then follow an arbitrary number of guesses, each also represented as N integers, each in the range 1 to 9. Following the last guess in each game will be N zeroes; these zeroes are not to be considered as a guess. Following the data for the first game will appear data for the second game (if any) beginning with a new value for N. The last game in the input will be followed by a single ‘0’ (when a value for N would normally be specified). The maximum value for N will be 1000.Output
The output for each game should list the hints that would be generated for each guess, in order, one hint per line. Each hint should be represented as a pair of integers enclosed in parentheses and separated by a comma. The entire list of hints for each game should be prefixed by a heading indicating the game number; games are numbered sequentially starting with 1. Look at the samples below for the exact format. 思路: 用两个数组分别装答案序列和用户猜的序列。 记录相同位置且数相同的个数x; 遍历1~9,统计二者出现的次数h1和h2,y+=min(h1,h2)得到总的重复个数。 y-x即:数字在两个序列都出现过但位置不对的个数。#includeusing namespace std;int arr1[1001], arr2[1001], num = 1;int main(){ int n; while (cin >> n) { if (n == 0) { break; } for (int i = 0; i < n; i++) { cin >> arr1[i]; // 输入答案序列 } printf("Game %d:\n", num++); while (1) { int x = 0, y = 0; for (int i = 0; i < n; i++) { cin >> arr2[i]; // 输入用户猜的序列 if (arr1[i] == arr2[i]) // 记录相同位置相同数的个数. { x++; } } if (arr2[0] == 0) // 一定要输入完成后在判断 如果输入一个0就退出循环了 那后面输入的数会留在缓冲区影响下次循环 { break; } for (int i = 1; i <= 9; i++) // 用h1,h2记录数字为i分别在两个序列中的个数 { int h1 = 0, h2 = 0; for (int j = 0; j < n; j++) { if (arr2[j] == i) { h2++; } if (arr1[j] == i) { h1++; } } y += h1 > h2 ? h2 : h1; // 用y记录数字为i的两个序列中最少的个数,方便y-x计算数字在两个序列都出现过但位置不对的个数。 } cout << " (" << x << "," << y - x << ")" << endl; } } return 0;}
转载地址:http://lgyof.baihongyu.com/