Problem: 1013 User: nastaMemory: 704K Time: 16MSLanguage: G++
Result: Accepted
普通的枚举题,水题一个,但是还是要仔细想好算法,要不然会WA的。
算法:
如果结果为“even”,则在其中的硬币均为真币,反之则不然,用real[]数组记录硬币是否为真,用lh[]数组记录重量情况,如果轻就--,重就++,在lh[]中绝对值最大的就是假币了。
开始在CodeBlocks编译通过了,提交后居然compiled error,google了一下原来是没加 #include <string> ,我还以为string类是在<iostream>中,汗个。。。
#include <iostream> #include <string> using namespace std; int abs(int n) { if(n < 0) return -n; return n; } int isEq(const string s1, const string s2, const string s3, const char c) { int size; int i, j; size = s1.size(); for(i = 0;i < size;i++) { if(s1[i] == c || s2[i] == c) { if(s3 == "even") return 'e'; else if(s3 == "down") { if(s2[i] == c) return 'h'; else return 'l'; } else if(s3 == "up") { if(s2[i] == c) return 'l'; else return 'h'; } } } return false; } int main() { // freopen("in.txt", "r", stdin); int n, i, j, k; string s[3][3], output; cin >> n; for(i = 0;i < n;i++) { bool real[12] = {0}; int lh[12] = {0}; int state, max = 0; for(j = 0; j < 3;j++) cin >> s[j][0] >> s[j][1] >> s[j][2]; for(j = 0; j <= 12;j++) { for(k = 0; k < 3; k++) { state = isEq(s[k][0], s[k][1], s[k][2], j + 'A'); if(state == 'e') { real[j] = true; break; } else if(state == 'l') lh[j]--; else if(state == 'h') lh[j]++; } } for(k = 0;k < 12;k++) { // cout << (char) (k + 'A') << " " << real[k] << " " << lh[k] << endl; if(real[k]) continue; else { if(abs(lh[k]) > abs(lh[max]) || real[max]) max = k; } } if(lh[max] > 0) output = "heavy"; else if(lh[max] < 0) output = "light"; cout << (char) (max + 'A') << " is the counterfeit coin and it is " << output << "." <<endl; } // fclose(stdin); return 0; }