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;
    }
  • 相关阅读:
    tar 解压缩命令
    utf8转为gb2312的函数
    .bash_profile和.bashrc的区别(如何设置生效)
    shell脚本操作mysql数据库—创建数据库,在该数据库中创建表(插入,查询,更新,删除操作也可以做)
    CentOS查看系统信息命令和方法
    SSL Programming Tutorial
    高性能、高并发TCP服务器(多线程调用libevent)
    phpmyadmin数据导入最大限制的解决方法
    微信分享网页的缩略图
    微信公众账号添加关注按钮和分享到朋友圈按钮.
  • 原文地址:https://www.cnblogs.com/0803yijia/p/2369199.html
Copyright © 2011-2022 走看看