标签:
題目:給你12個硬幣編號A-L,有一個和其他的重量不一樣,現在取某些硬幣放到天平兩端;
給你3中不同的測試結果,為哪個是特殊的硬幣是輕了還是重了(一定有唯一解)。
分析:暴力、枚舉。數據較小直接枚舉24中可能性,判斷條件是否成立即可。
說明:最近做題很少啊╮(╯▽╰)╭。
#include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int coins[12]; char str_a[3][13],str_b[3][13],str_c[3][13]; int state(char s[]) { if (!strcmp(s, "even")) return 0; if (!strcmp(s, "up")) return 1; if (!strcmp(s, "down")) return -1; return -2; } int compare(char a[], char b[]) { int value_a = 0,value_b = 0; for (int i = 0; a[i]; ++ i) value_a += coins[a[i]-'A']; for (int i = 0; b[i]; ++ i) value_b += coins[b[i]-'A']; return value_a-value_b; } int main() { int n,s; while (~scanf("%d",&n)) while (n --) { for (int i = 0; i < 3; ++ i) scanf("%s %s %s",str_a[i],str_b[i],str_c[i]); for (int p = 0; p < 12; ++ p) for (int q = 0; q <= 1; ++ q) { for (int i = 0; i < 12; ++ i) coins[i] = 0; if (q == 1) { coins[p] = -1; }else coins[p] = 1; int flag = 1; for (int i = 0; i < 3; ++ i) { if (compare(str_a[i], str_b[i]) != state(str_c[i])) { flag = 0; break; } } if (flag) { for (int i = 0; i < 12; ++ i) { if (coins[i] == -1 || coins[i] == 1) { printf("%c is the counterfeit coin and it is ",'A'+i); if (coins[i] < 0) { printf("light.\n"); }else printf("heavy.\n"); break; } } } } } return 0; }
标签:
原文地址:http://blog.csdn.net/mobius_strip/article/details/45094765