zoukankan      html  css  js  c++  java
  • Ballot evaluation 注意精度问题

    很郁闷。。。一直以为是精度问题。。。其实还就是精度问题=  =。。。另外还有一个问题就是看错题了,最后的那个数字我写成了double 型。后来改正后就AC了= =。。。刘老师不在的话,我想说一句“我怀念我的103小马甲= =”

     

    Ballot evaluation

    Time Limit: 1000MS Memory limit: 65536K

    题目描述

    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.
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    typedef struct
    {
    char name[25];
    double dig;
    }party;
    party man[100];
    const double eps = 1e-6;
    int cmp(double x)
    {
    if(x > eps) return 1;
    else if(x < -eps) return -1;
    else return 0;
    }
    int judge(int t,double s,int n)
    {
    switch(t)
    {
    case 1:if(cmp(s-n)<0)return 1;break;
    case 2:if(cmp(s-n)<=0)return 1;break;
    case 3:if(cmp(s-n)>0)return 1;break;
    case 4:if(cmp(s-n)>=0)return 1;break;
    case 5:if(cmp(s-n) == 0)return 1;break;
    }
    return 0;
    }
    int main()
    {
    int p,g,n,i,j,c1;
    char c[10000];
    int t,x;
    double sum;
    scanf("%d %d",&p,&g);
    for(i = 0;i < p;i++)
    {
    scanf("%s %lf",man[i].name,&man[i].dig);
    }
    for(i = 1;i <= g;i++)
    {
    sum = 0;
    while(scanf("%s",c)!=EOF)
    {
    if(strcmp(c,"+")!=0)
    {
    if(strcmp(c,"<")==0)
    { t = 1;break;}
    else if(strcmp(c,"<=")==0)
    { t= 2;break;}
    else if(strcmp(c,">")==0)
    { t = 3;break;}
    else if(strcmp(c,">=")==0)
    { t = 4;break;}
    else if(strcmp(c,"=")==0)
    { t = 5;break;}
    else
    {
    for(j = 0;j<p;j++)
    if(strcmp(c,man[j].name)==0)
    {
    sum+=man[j].dig;
    break;
    }
    }
    }
    }
    scanf("%d",&c1);
    x = judge(t,sum,c1);
    if(x)
    printf("Guess #%d was correct.\n",i);
    else
    printf("Guess #%d was incorrect.\n",i);
    }

    return 0;
    }
  • 相关阅读:
    SQLSERVER 2008 编辑所有或者任意行
    在SQL2008和2012里面怎么让显示全部行和编辑 全部而不是200和1000
    sql server 提取汉字/数字/字母的方法
    [再寄小读者之数学篇](2014-05-12 曲线的弧长计算)
    [复变函数]第23堂课 6.2 用留数定理计算实积分 (续)
    [再寄小读者之数学篇](2014-04-23 行列式的导数)
    [再寄小读者之数学篇](2014-04-22 平方差公式在矩阵中的表达)
    康乐不风流之爱解题的pde灌水王张祖锦
    [复变函数]第22堂课 6.2 用留数定理计算实积分
    [Tex学习笔记]数学公式再次测试
  • 原文地址:https://www.cnblogs.com/0803yijia/p/2369199.html
Copyright © 2011-2022 走看看