HDU 2986 Ballot evaluation(精度问题)

点我看题目

题意 : 给你n个人名,每个名后边跟着一个数,然后m个式子,判断是否正确。

思路 :算是一个模拟吧,但是要注意浮点数容易丢失精度,所以要好好处理精度,不知道多少人死在精度上,不过我实在是不怎么会处理精度,所以我就让那个数变为字符串输入然后在处理,相当于乘上10,但是直接乘上10,数容易变,不知道的自己可以试一下。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <string>
#include <map>
#include <algorithm>

using namespace std;
string name[51] ;
string score ;
char ch[51] ;
int yun ;
int main()
{
    int p,g ,x,m;
    scanf("%d %d",&p,&g) ;
    map<string,int >mp ;
    for(int i = 0 ; i < p ; i++)
    {
        cin>>name[i]>>score ;
        m = 0 ;
        int len = score.size() ;
        m += score[len-1]-'0' ;
        int j;
        for(j = 0 ; j < len ; j++)
        if(score[j] == '.') break ;
        int n = 10 ;
      //  printf("%d*
",j) ;
        for(int k = j-1 ; k >= 0 ; k--)
        {
            m += (score[k]-'0')*n ;
            n = n*10 ;
        }
        mp[name[i]] = m ;
       // printf("#%d#
",mp[name[i]]) ;
    }
    for(int i = 1 ; i <= g ; i++)
    {
        int sum = 0 ;
        while(true)
        {
            scanf("%s",ch) ;
            if(ch[0] == '=' || ch[0] == '>'||ch[0] == '<')
           {
               if(ch[0] == '=')
               yun = 3 ;
               else if(ch[0] == '>' && ch[1] == '=' )
               yun = 4 ;
               else if(ch[0] == '<' && ch[1] == '=')
               yun = 5 ;
               else if(ch[0] == '>')
               yun = 1 ;
               else if(ch[0] == '<')
               yun = 2 ;
               break ;
           }
            if(ch[0] != '+')
            sum += mp[ch] ;
        }
        scanf("%d",&x) ;
        x *= 10 ;
       // printf("%d %d
",sum,x) ;
        if(yun == 1)
        {
            if(sum > x)
            printf("Guess #%d was correct.
",i) ;
            else printf("Guess #%d was incorrect.
",i) ;
        }
        if(yun == 2)
        {
            if(sum < x)
            printf("Guess #%d was correct.
",i) ;
            else printf("Guess #%d was incorrect.
",i) ;
        }
        if(yun == 3)
        {
            if(sum == x)
            printf("Guess #%d was correct.
",i) ;
            else printf("Guess #%d was incorrect.
",i) ;
        }
        if(yun == 4)
        {
            if(sum >= x)
            printf("Guess #%d was correct.
",i) ;
            else printf("Guess #%d was incorrect.
",i) ;
        }
        if(yun == 5)
        {
            if(sum <= x)
            printf("Guess #%d was correct.
",i) ;
            else printf("Guess #%d was incorrect.
",i) ;
        }
    }
    return 0 ;
}
View Code
原文地址:https://www.cnblogs.com/luyingfeng/p/3633466.html