SDUT2389Ballot evaluation

这个题就是有一个精度误差的问题 其它还好

题目描述
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.
 1 #include<stdio.h>
2 #include<string.h>
3 typedef struct node
4 {
5 double data;
6 char p[25];
7 int flag;
8 }st;
9 const double eps = 1e-6;
10 int cmp(double x)
11 {
12 if(x > eps) return 1;
13 else if(x < -eps) return -1;
14 else return 0;
15 }
16 int judge(char str[],double s,int n)
17 {
18 int k;
19 k = strlen(str);
20 if(k == 1)
21 {
22 switch(str[0])
23 {
24 case '<':if(cmp(s-n)<0)return 1;break;
25 case '>':if(cmp(s-n)>0)return 1;break;
26 case '=':if(cmp(s-n) == 0)return 1;break;
27 }
28 }
29 else
30 {
31 switch(str[0])
32 {
33 case '<':if(cmp(s-n)<=0)return 1;break;
34 case '>':if(cmp(s-n)>=0)return 1;break;
35 }
36 }
37 return 0;
38 }
39 int main()
40 {
41 int t,g,n,i,x,k = 0,j;
42 st a[55];
43 double s;
44 char str[25];
45 scanf("%d %d",&t,&g);
46 for(i = 1 ; i <= t ; i++)
47 scanf("%s %lf",a[i].p,&a[i].data);
48 while(g--)
49 {
50 k++;
51 s = 0;
52 for(j = 1 ; j <= t ; j++)
53 a[j].flag = 0;
54 while(scanf("%s",str)!=NULL)
55 {
56 if(str[0]!='+'&&str[0]!='<'&&str[0]!='>'&&str[0]!='=')
57 {
58 for(i = 1 ; i <= t ; i++)
59 if(a[i].flag==0)
60 {
61 if(strcmp(str,a[i].p) == 0)
62 {
63 s = s+a[i].data;
64 a[i].flag = 1;
65 }
66 }
67 }
68 else
69 if(str[0]!='+')
70 {
71 scanf("%d",&n);
72 x = judge(str,s,n);
73 break;
74 }
75
76 }
77 if(x)
78 printf("Guess #%d was correct.\n",k);
79 else
80 printf("Guess #%d was incorrect.\n",k);
81 }
82 return 0;
83 }



posted @ 2012-02-26 21:01  _雨  阅读(238)  评论(0编辑  收藏  举报