2016-07-22 Problem Solving►UVa UVa 1587 - Box Contents 1. Problem2. Solution3. Code Problem題目網址給六塊平面,看可不可以組成一個矩形。 Solution兩個寫法概念一樣,都是先去找兩兩成對一樣的平面(會有 3 對),再去判斷這 3 種不同平面能不能組成矩形。 Code(1) UVa 1587123456789101112131415161718192021222324252627282930313233343536373839404142434445464748#include<cstdio>#include<algorithm>using namespace std;struct Pallet{ int w, h;}pallet[6];int main(){ int w, h; while (scanf("%d%d", &w, &h) != EOF) { if (w < h) pallet[0].w = w, pallet[0].h = h; else pallet[0].w = h, pallet[0].h = w; int i; for (i = 1; i < 6; i++) { scanf("%d%d", &w, &h); if (w < h) pallet[i].w = w, pallet[i].h = h; else pallet[i].w = h, pallet[i].h = w; } sort(pallet, pallet + 6, [](Pallet& a, Pallet& b)->bool{return a.w < b.w || ((a.w == b.w) && (a.h < b.h)); }); bool ok = true; for (i = 0; i < 6; i += 2) if (pallet[i].w != pallet[i + 1].w || pallet[i].h != pallet[i + 1].h) ok = false; if (ok) { ok = false; if (pallet[0].w == pallet[2].w && pallet[0].h == pallet[4].w&&pallet[2].h == pallet[4].h) ok = true; } puts(ok ? "POSSIBLE" : "IMPOSSIBLE"); } return 0;} (2)UVa 1587123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475#include<cstdio>int main(){ int w, h, pallet[6][2] = {}; while (scanf("%d%d", &w, &h) != EOF) { if (w < h) pallet[0][0] = w, pallet[0][1] = h; else pallet[0][0] = h, pallet[0][1] = w; int i; for (i = 1; i < 6; i++) { scanf("%d%d", &w, &h); if (w < h) pallet[i][0] = w, pallet[i][1] = h; else pallet[i][0] = h, pallet[i][1] = w; } bool used[6] = {}, ok = true; int side[3][2], count = 0; //看是否兩兩成對,並找不一樣的 3 塊 for (i = 0; i < 6; i++) { if (used[i]) continue; for (int j = i + 1; j < 6; j++) if (!used[j] && pallet[i][0] == pallet[j][0] && pallet[i][1] == pallet[j][1]) { used[i] = used[j] = true; break; } if (!used[i]) break; side[count][0] = pallet[i][0]; side[count++][1] = pallet[i][1]; } for (i = 0; i < 6; i++) if (!used[i]) ok = false; if (ok) { ok = false; //用那 3 塊去拚看看矩形的 上方一片、側邊兩片 for (i = 0; i <= 1; i++) { for (int j = 0; j <= 1; j++) { if (side[0][i] == side[1][j]) { if (side[0][!i] == side[2][0] && side[2][1] == side[1][!j]) ok = true; else if (side[0][!i] == side[2][1] && side[2][0] == side[1][!j]) ok = true; } } } } puts(ok ? "POSSIBLE" : "IMPOSSIBLE"); } return 0;} Newer UVa 619 - Numerically Speaking Older UVa 202 - Repeating Decimals