Problem Description 题目链接
Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistinguishable from the real silver dollars. The counterfeit coin has a different weight from the other coins but Sally does not know if it is heavier or lighter than the real coins.
Happily, Sally has a friend who loans her a very accurate balance scale. The friend will permit Sally three weighings to find the counterfeit coin. For instance, if Sally weighs two coins against each other and the scales balance then she knows these two coins are true. Now if Sally weighs one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed goes up or down, respectively.
By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings.
Input
Output
Sample Input
Sample Output
思路:这道题数据量小,采用暴力遍历所有可能的24种情况,12枚硬币*2个情况(要么重了,要么轻了)=24
判断函数主要是四个判断:
- 字符出现在‘e’平等的情况下,则是合格的。
- 字符如果前面出现'u‘后面出现'd',说明这个字符是合格的。
- 如果在没有这个字符的情况下还出现重量不平等的情况,说明这个字符是合格。
- 这个字符可能未出现过,也说明合格。
#include <iostream> #include <string> #include <cstring> using namespace std; char strs[3][3][15]; bool Judge(char point, int weight) { int tag = 0, flag = 0; //用于标记 char ch; for (int j = 0; j < 3; j++) { ch = strs[j][2][0]; tag = 0; for (int i = 0; i < 2; i++) { for (int k = 0; strs[j][i][k] != '