hdu 2986 Ballot evaluation (Simulation)
之前在华工赛见过的一道简单的模拟,用map轻松干掉。为了精确,要全程用整型比较。轻松1y~
代码如下:
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <string> 6 #include <map> 7 8 using namespace std; 9 10 map<string, int> val; 11 12 int con(char *str) { 13 int a, b; 14 sscanf(str, "%d.%d", &a, &b); 15 return a * 10 + b; 16 } 17 18 bool check(int a, int b, char *p) { 19 if (!strcmp(p, "=")) return a == b; 20 if (!strcmp(p, "<=")) return a <= b; 21 if (!strcmp(p, ">=")) return a >= b; 22 if (!strcmp(p, "<")) return a < b; 23 if (!strcmp(p, ">")) return a > b; 24 return false; 25 } 26 27 int main() { 28 int n, m; 29 char buf[2][100]; 30 while (cin >> n >> m) { 31 for (int i = 0; i < n; i++) { 32 for (int i = 0; i < 2; i++) cin >> buf[i]; 33 val[buf[0]] = con(buf[1]); 34 } 35 for (int cas = 1; cas <= m; cas++) { 36 int sum = 0; 37 while (true) { 38 cin >> buf[0]; 39 sum += val[buf[0]]; 40 cin >> buf[0]; 41 if (buf[0][0] != '+') break; 42 } 43 int x; 44 cin >> x; 45 cout << "Guess #" << cas << " was " << (check(sum, x * 10, buf[0]) ? "correct." : "incorrect.") << endl; 46 } 47 } 48 return 0; 49 }
——written by Lyon