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*\n",j) ;
        for(int k = j-1 ; k >= 0 ; k--)
        {
            m += (score[k]-'0')*n ;
            n = n*10 ;
        }
        mp[name[i]] = m ;
       // printf("#%d#\n",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\n",sum,x) ;
        if(yun == 1)
        {
            if(sum > x)
            printf("Guess #%d was correct.\n",i) ;
            else printf("Guess #%d was incorrect.\n",i) ;
        }
        if(yun == 2)
        {
            if(sum < x)
            printf("Guess #%d was correct.\n",i) ;
            else printf("Guess #%d was incorrect.\n",i) ;
        }
        if(yun == 3)
        {
            if(sum == x)
            printf("Guess #%d was correct.\n",i) ;
            else printf("Guess #%d was incorrect.\n",i) ;
        }
        if(yun == 4)
        {
            if(sum >= x)
            printf("Guess #%d was correct.\n",i) ;
            else printf("Guess #%d was incorrect.\n",i) ;
        }
        if(yun == 5)
        {
            if(sum <= x)
            printf("Guess #%d was correct.\n",i) ;
            else printf("Guess #%d was incorrect.\n",i) ;
        }
    }
    return 0 ;
}
View Code

 

转载于:https://www.cnblogs.com/luyingfeng/p/3633466.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值