POJ-1013
2012-05-16 22:14
274 查看
Counterfeit Dollar
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
The first line of input is an integer n (n > 0) specifying the number of cases to follow. Each case consists of three lines of input, one for each weighing. Sally has identified each of the coins with the letters A--L. Information on a weighing will be given
by two strings of letters and then one of the words ``up'', ``down'', or ``even''. The first string of letters will represent the coins on the left balance; the second string, the coins on the right balance. (Sally will always place the same number of coins
on the right balance as on the left balance.) The word in the third position will tell whether the right side of the balance goes up, down, or remains even.
Output
For each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light. The solution will always be uniquely determined.
Sample Input
Sample Output
Source
East Central North America 1998
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32440 | Accepted: 10228 |
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
The first line of input is an integer n (n > 0) specifying the number of cases to follow. Each case consists of three lines of input, one for each weighing. Sally has identified each of the coins with the letters A--L. Information on a weighing will be given
by two strings of letters and then one of the words ``up'', ``down'', or ``even''. The first string of letters will represent the coins on the left balance; the second string, the coins on the right balance. (Sally will always place the same number of coins
on the right balance as on the left balance.) The word in the third position will tell whether the right side of the balance goes up, down, or remains even.
Output
For each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light. The solution will always be uniquely determined.
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 <stdio.h> #include <string.h> int main() { int cases, i, j, k, n, coins[12] = {0}, suml, sumr, ok; char balance[3][3][12]; scanf("%d", &cases); while (cases--) { for (i = 0; i < 3; i++) { scanf("%s%s%s", balance[i][0], balance[i][1], balance[i][2]); } for (i = 0, ok = 1; i < 12 && ok; i++) { for (n = 1; n >= -1 && ok; n-=2) { coins[i] = n; for (j = 0; j < 3; j++) { for (k = suml = 0; balance[j][0][k]; k++) suml += coins[balance[j][0][k]-'A']; for (k = sumr = 0; balance[j][1][k]; k++) sumr += coins[balance[j][1][k]-'A']; if (!strcmp(balance[j][2], "even") && suml != sumr) break; if (!strcmp(balance[j][2], "up") && suml <= sumr) break; if (!strcmp(balance[j][2], "down") && suml >= sumr) break; } if (j == 3) { ok = 0; if (coins[i] == 1) printf("%c is the counterfeit coin and it is heavy.\n", i+'A'); else printf("%c is the counterfeit coin and it is light.\n", i+'A'); } } coins[i] = 0; } } return 0; }
相关文章推荐
- POJ1013 Counterfeit Dollar 解题报告
- poj 1013 Counterfeit Dollar(模拟)
- Poj-1013 Counterfeit Dollar (Runtime Error)
- POJ1013 称硬币
- HDU 1482/ZOJ 1184/FOJ 1003/POJ 1013 Counterfeit Dollar
- POJ 1013 - Counterfeit Dollar(模拟)
- POJ1013解题报告
- POJ 1013 Counterfeit Dollar 砝码称重
- poj1013,Counterfeit Dollar
- poj 1013 Counterfeit Dollar
- hdoj 1482 poj 1013
- poj 1013 称硬币
- 模拟,找次品硬币,Counterfeit Dollar(POJ 1013)
- POJ 1013 (简单模拟)
- 硬币称重问题之POJ1013-Counterfeit Dollar
- poj 1013 Counterfeit Dollar 已知12个球中有1个重量与众不同,用3次找出此球,并判断重量轻重
- POJ 1013 Counterfeit Dollar(水~)
- poj 1013 称硬币
- POJ1013 Counterfeit Dollar
- poj 1013 Counterfeit Dollar