sdutacm 2389 Ballot evaluation

http://acm.sdut.edu.cn/web/problem.php?action=showproblem&problemid=2389

题意:判断表达式是否成立;精度要求非常高;

代码:

View Code
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
struct node
{
char str[25];
double data;
}link[55];
char s[25] = {NULL};
double ee = 1e-8;
int n = 0;
int qu = 0;
double res = 0;
void print1(int i)
{
printf("Guess #%d was correct.\n",i);
}
void print2(int i)
{
printf("Guess #%d was incorrect.\n",i);
}
int main()
{
scanf("%d %d",&n,&qu);
for(int i = 0; i < n; ++i)
{
scanf("%s %lf",link[i].str,&link[i].data);
}
int ok = 0;
for(int k = 1; k <= qu; ++k)
{
scanf("%s",s);
ok = 0;
bool ans = 1;
double temp = 0;
for(int i = 0; i <n; ++i)
if(0 == strcmp(link[i].str,s))
{
temp = link[i].data;
break;
}
while(1)
{
scanf("%s ",s);
if(0 == strcmp(s,"+"))
{
double tt = 0;
scanf("%s ",s);
for(int i = 0; i <n; ++i)
if(0 == strcmp(link[i].str,s))
{
tt = link[i].data;
break;
}
temp = temp +tt;
}
else
if(!strcmp(s,">")||!strcmp(s,"<")||!strcmp(s,">=")||!strcmp(s,"<=")||!strcmp(s,"="))
{
scanf("%lf",&res);//这后面的精度问题,也挺好的,认识了点精度的解决方法;
if(!strcmp(s,">")) {if(temp - res > ee) print1(k); else print2(k); ok = 1;}
else if(!strcmp(s,"<")) {if(res - temp > ee ) print1(k); else print2(k); ok = 1;}
else if(!strcmp(s,">=")) {if(!(res - temp > ee)) print1(k); else print2(k); ok = 1;}
else if(!strcmp(s,"<=")){if(!(temp - res > ee)) print1(k); else print2(k); ok = 1;}
else if(!strcmp(s,"=")){if(!(temp -res > ee )&& !(res - temp > ee) )print1(k); else print2(k); ok = 1;}
}
if(ok)
break;
}
}
return 0;
}





posted @ 2012-02-26 20:52  LT-blogs  阅读(186)  评论(0编辑  收藏  举报