Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 36273 | Accepted: 11583 |
Description
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
1 ABCD EFGH even ABCI EFJK up ABIJ EFGH even
Sample Output
K is the counterfeit coin and it is light.
Source
假币
Time Limit: 1000MS | Memory Limit: 10000K |
Total Submissions: 36273 | Accepted: 11583 |
Description
Sally Jones有一打银币(12个)。然而,只有11个硬币是真的银币;有一个假币颜色和形状都跟真的一样。假的这个只有重量和真的不一样,不过Sally搞不清楚假的是轻了还是重了。
令人欣喜的是,Sally有一个朋友借给她了一个碉堡了的天平。她的朋友允许她称量三次来找到假币,如果Sally两边各放一枚银币,如果天平平衡了,说明这2个全真的,然后Sally拿一个真的和第三个各放一边,如果天平没平衡,则Sally知道这第三个硬币是假的,并且知道这枚假币是轻了还是重了。
通过合理的承重,她认为可以在三次称重中找到假币。
Input
第一行输入一个n(n>0)说明一共有多少组数据。每一组数据包含三行输入,包括一次称重过程。Sally已经将所有的银币标记为A—L。每次的称重信息包含两个由字母组成的字符串和一个单词“up”,”down”,或者”even”。第一串字母表示天平左面的银币;第二串字母表示天平右面的银币。(Sally每次将在左面和右面放等量的银币。)占据第三个位置的那个单词江表示右边盘子内的银币相对于左边盘子是沉了,轻了,还是一样重
Output
对于每一组数据,输出包括假币对应的字母,和他的重量(轻了还是重了)结果总是单一的
Sample Input
1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even
Sample Output
K is the counterfeit coin and it is light.
Source
East Central North America 1998
这道题算逻辑推理么.....额,感觉更像个模拟,其实就是如果两边平衡那就肯定都是真的没跑了,就都强制写上:真!。然后如果有重量差别,那左右哪个都有可能是假的,那就打个标记,重的一边加一,轻的一边减一。
#include<iostream>
#include<cmath>
#include<algorithm>
#include <stdio.h>
#include <string.h>
int main(void)
{
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
int n;
scanf("%d", &n);
// printf("%d", n);
while(n--)
{
// printf("%d", k);
char left[14]={0}, right[14]={0}, weight[5]{0};
int distrust[14] = {0};
int trust[14] = {0};
for (int j = 0;j < 3;j ++)
{
scanf("%s%s%s", left, right, weight);
// printf("%s %s %s\n", left, right, weight);
if (weight[0] == 'e')
{
for (int i = 0;i < strlen(left);i ++)
{
trust[left[i] - 'A'] = 1;
trust[right[i] - 'A'] = 1;
}
}
if (weight[0] == 'u')
{
for (int i = 0;i < strlen(left);i ++)
{
distrust[left[i] - 'A']--;
distrust[right[i] - 'A']++;
}
}
if (weight[0] == 'd')
{
for (int i = 0;i < strlen(left);i ++)
{
distrust[left[i] - 'A']++;
distrust[right[i] - 'A']--;
}
}
}
int maxdis = -1;
int maxreal = -1;
for (int i = 0;i < 12;i ++)
{
// printf("%d ", distrust[i]);
if (trust[i]) continue;
if (abs(distrust[i]) > maxdis)
{
maxdis = abs(distrust[i]);
maxreal = i;
}
}
if (distrust[maxreal] > 0)
printf("%c is the counterfeit coin and it is light.\n", maxreal + 'A');
else printf("%c is the counterfeit coin and it is heavy.\n", maxreal + 'A');
}
}