HDU 2986 Ballot evaluation(精度问题)

时间:2022-06-01 12:37:12

点我看题目

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

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

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <string>
#include <map>
#include <algorithm> using namespace std;
string name[] ;
string score ;
char ch[] ;
int yun ;
int main()
{
int p,g ,x,m;
scanf("%d %d",&p,&g) ;
map<string,int >mp ;
for(int i = ; i < p ; i++)
{
cin>>name[i]>>score ;
m = ;
int len = score.size() ;
m += score[len-]-'' ;
int j;
for(j = ; j < len ; j++)
if(score[j] == '.') break ;
int n = ;
// printf("%d*\n",j) ;
for(int k = j- ; k >= ; k--)
{
m += (score[k]-'')*n ;
n = n* ;
}
mp[name[i]] = m ;
// printf("#%d#\n",mp[name[i]]) ;
}
for(int i = ; i <= g ; i++)
{
int sum = ;
while(true)
{
scanf("%s",ch) ;
if(ch[] == '=' || ch[] == '>'||ch[] == '<')
{
if(ch[] == '=')
yun = ;
else if(ch[] == '>' && ch[] == '=' )
yun = ;
else if(ch[] == '<' && ch[] == '=')
yun = ;
else if(ch[] == '>')
yun = ;
else if(ch[] == '<')
yun = ;
break ;
}
if(ch[] != '+')
sum += mp[ch] ;
}
scanf("%d",&x) ;
x *= ;
// printf("%d %d\n",sum,x) ;
if(yun == )
{
if(sum > x)
printf("Guess #%d was correct.\n",i) ;
else printf("Guess #%d was incorrect.\n",i) ;
}
if(yun == )
{
if(sum < x)
printf("Guess #%d was correct.\n",i) ;
else printf("Guess #%d was incorrect.\n",i) ;
}
if(yun == )
{
if(sum == x)
printf("Guess #%d was correct.\n",i) ;
else printf("Guess #%d was incorrect.\n",i) ;
}
if(yun == )
{
if(sum >= x)
printf("Guess #%d was correct.\n",i) ;
else printf("Guess #%d was incorrect.\n",i) ;
}
if(yun == )
{
if(sum <= x)
printf("Guess #%d was correct.\n",i) ;
else printf("Guess #%d was incorrect.\n",i) ;
}
}
return ;
}