首页 > 其他 > 详细

HDU 2986 Ballot evaluation(精度问题)

时间:2014-03-30 12:10:20      阅读:466      评论:0      收藏:0      [点我收藏+]

点我看题目

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

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

bubuko.com,布布扣
bubuko.com,布布扣
#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
bubuko.com,布布扣

HDU 2986 Ballot evaluation(精度问题),布布扣,bubuko.com

HDU 2986 Ballot evaluation(精度问题)

原文:http://www.cnblogs.com/luyingfeng/p/3633466.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!