hdu 2986 Ballot evaluationp

题意:前面p行一个字符窜代表一个数(一个小数字),下面q行,让你判断字符窜相加和后面给的判断是否一致,如果一致则输出正确,否则错误

这道题小心再精度,先将小数乘以10换成整数比较

思路:输入的数据当成字符窜进行存储ch1,
然后拆分ch1,读入到ch2中,一个名字定义
一个数组,当遇到空格,说明一个单词存储
完毕,输入结束符。遇到+就跳过


#include <stdio.h>
#include <string.h>
#include<math.h>
char ch2[100][50];
struct Node
{
	char name[22];
	double num;
};
Node list[100];
int main(int argc, char *argv[])
{
	int p,g,i,j,t;
	char ch1[500];
	double Rem,sum;
	scanf("%d%d",&p,&g);
	for (i=0;i<p;i++)
	{
		scanf("%s%lf",list[i].name,&list[i].num);
	    list[i].num*=10;//
	}
	getchar();
	  int cas1,cas2;
	for (j=0;j<g;j++)//猜想几组 
	{
		sum=0;
		Rem=0;
		cas1=0;
		cas2=0;
		gets(ch1);
		for (i=0;ch1[i];i++)
		{
			if(ch1[i]=='+') 
			{
				i++;
			 	continue;
			}
			if(ch1[i]==' ') 
				{
			        ch2[cas1][cas2]='\0';
			        cas2=0;
                    cas1++; 
					continue;
				}
				ch2[cas1][cas2++]=ch1[i];
			
		}
		ch2[cas1][cas2]='\0';
		int len;
		for (len=0;ch2[cas1][len];len++);
		for (t=0;ch2[cas1][t];t++)
		{
			sum+=(ch2[cas1][t]-'0')*pow((double)10,len-1);
			len--;
		}
		sum*=10;//
		for (int k=0;k<cas1-1;k++)//几个相加 
		{
			for (i=0;i<p;i++)//遍历前面的list 
			  if(!strcmp(list[i].name,ch2[k]))  
			  {
  			     Rem+=list[i].num;	
				 break;
  			  }
		}
		printf("Guess #%d ",j+1);
		if(!strcmp(ch2[cas1-1],">"))
		{
			if(Rem>sum) printf("was correct.\n");
			else printf("was incorrect.\n");
		}
		else if(!strcmp(ch2[cas1-1],"<"))
		{
			if(Rem<sum) printf("was correct.\n");
			else printf("was incorrect.\n");
		}
		else if(!strcmp(ch2[cas1-1],">="))
		{
			if(Rem>=sum) printf("was correct.\n");
			else printf("was incorrect.\n");
		}
		else if(!strcmp(ch2[cas1-1],"<="))
		{
			if(Rem<=sum) printf("was correct.\n");
			else printf("was incorrect.\n");
		}
		else if(!strcmp(ch2[cas1-1],"="))
		{
			if(Rem==sum) printf("was correct.\n");
			else printf("was incorrect.\n");
		}
	}  
	return 0;
}


 

你可能感兴趣的:(hdu 2986 Ballot evaluationp)