zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    梯度下降(一)
    springcloud中的gateway中的配置访问方式
    hyper-v powershell 开通虚拟机,配置vlan,配置CPU,内存
    DOCKER 安装zabbix_proxy进行代理监控
    LINUX lvm扩容
    ipset 如何在 Linux 下大量屏蔽恶意 IP 地址
    变量、常量和注释
    pycharm使用和虚拟环境
    编程语言和python介绍及安装
    Spring详解(三)——认识IoC控制反转/DI依赖注入
  • 原文地址:https://www.cnblogs.com/luyingfeng/p/3633466.html
Copyright © 2011-2022 走看看