您的位置:首页 > 编程语言 > Lua

UIM:Ballot evaluation

2015-08-05 19:56 489 查看
题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=3906

题目描述:

Before the 2009 elections at the European Parliament, Bill and Ted have asked their friends to
make guesses about the outcome of the ballot. Now, the results have been published, so Bill and Ted want to check who was right. But checking the results of their many friends would take a very long time, and they need the evaluation to be done by a computer.
Since they are not so good at programming, they ask you for help.

输入格式:

The
data provided by Bill and Ted has the following format: The first line consists of the number p of
parties followed by the number g of
guesses (with 1 ≤ p ≤ 50 and 1
≤ g ≤ 10000). Then follow p lines,
each line consisting of a unique party name of length ≤ 20 (only containing letters a-z, A-Z and digits 0-9) and the achieved vote percentage of this party with one digit after the decimal point.

After the parties follow g lines,
each consisting of a guess. A guess has the form P1 +
P2 + ... + Pk COMP n,
where P1 to Pk are
party names, COMP is
one of the comparison operators <, >, <=, >= or = and n is
an integer between 0 and 100, inclusively. Each party name occurs at most once in each guess.

输出格式:

For
each guess, sum up the vote percentages of the parties and compare them with the specified integer n.
Then, print a line stating whether the guess was correct. See the sample output for details.

输入样例:

6 5
CDU 30.7
SPD 20.8
Gruene 12.1
FDP 11.0
DIELINKE 7.5
CSU 7.2
FDP > 11
CDU + SPD < 50
SPD + CSU >= 28
FDP + SPD + CDU <= 42
CDU + FDP + SPD + DIELINKE = 70

输出样例:

Guess #1 was incorrect.
Guess #2 was incorrect.
Guess #3 was correct.
Guess #4 was incorrect.
Guess #5 was correct.

分析思路:

先用map将键值关系存下来,然后用gets读入一行guess信息,对信息中的左边和右边分别操作,算出左右的结果,然后根据中间得到的符号进行判定,如果符合,则正确,反之则反。另须注意的是浮点数大小的判定,开始就是不注意这点,随意四舍五入WA了一次,用两者差值与eps进行比较就可以得出两者的大小关系,一个sign函数的事。还有就是gets先读完再判定太麻烦了。。。下次一定用scanf或cin。

源代码如下:

//精度问题,实数判等。。。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<map>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 3000
#define eps 1e-8

int P, G;
char op[4], g[30], str[MAXN];
map<string, double> mp;

int sign(double a) {                       //浮点数判等函数
    return a < -eps ? -1 : a < eps ? 0 : 1;
}
int cmp(double sum, char *op, int num) {
    //printf("sum = %lf, num = %d\n", sum, num);
    if (strcmp(op, ">") == 0 && sign(sum - num) > 0) return 1;
    else if (strcmp(op, ">=") == 0 && sign(sum - num) >= 0) return 1;
    else if (strcmp(op, "<") == 0 && sign(sum - num) < 0) return 1;
    else if (strcmp(op, "<=") == 0 && sign(sum - num) <= 0) return 1;
    else if (strcmp(op, "=") == 0 && sign(sum - num) == 0) return 1;
    return 0;
}
int main() {
    FILE *p = freopen("test.txt", "r", stdin);
    int i, j, k, e, num;
    int c = 1;
    double per, sum;
    string ss;
    while (scanf("%d%d", &P, &G) != EOF) {
        for (i = 0; i < P; i++) {
            cin >> ss;
            scanf("%lf%*c", &per);
            mp[ss] = per;
        }
        for (i = 0; i < G; i++) {
            gets(str);
            j = 0; sum = 0;
            while (str[j] != '\0') {
                k = 0;
                while (str[j] != ' ') {
                    g[k++] = str[j];
                    j++;
                }
                g[k] = '\0';
                if (mp.count(g)) sum += mp[g];
                j++;
                if (str[j] != '+') {
                    e = 0;
                    while (str[j] != ' ') {
                        op[e++] = str[j];
                        j++;
                    }
                    op[e] = '\0';
                    j++;
                    num = 0;
                    while (str[j] != '\0') {
                        num = num * 10 + str[j] - '0';
                        j++;
                    }
                }
                else {
                    j += 2;
                }
            }
            int res = cmp(sum, op, num);
            printf("Guess #%d was %s\n", c++, res == 1 ? "correct." : "incorrect.");
        }
    }
    return 0;
}
运行结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: