Page 1 of 1

11629 - Ballot evaluation

Posted: Thu Oct 28, 2010 5:12 pm
by amine.hamdaoui
Hello Solver,

I try to solve the problem 11629, but i'm getting WA and I can't discover my fault. Can you help me?

Code: Select all

#include <map>
#include <iostream>
#include <string>
#include <iterator>
#include <sstream>

using namespace std;

int main()
{
    map<string,float>           parties;
    map<string,float>::iterator it;
    string                      name, scoreS, line;
    float                       score, to;
    int                         p, g;

    cin>>p>>g;
    getline(cin,line);

    // Reading and fetching parties names
    for(int i=0 ; i<p ; i++)
    {
        getline(cin, name,' ');
        getline(cin, scoreS);
        istringstream iss(scoreS);
        iss>>score;

        parties.insert(pair<string,float>(name,score));
    }

    // reading and solving Guess
    for(int i=1 ; i<=g ; i++)
    {
        // read the guess
        getline(cin,line);
        cout<<"Guess #"<<i<<" was ";
        score = 0;

        // parsing the guess
        istringstream iss(line);
        while(getline(iss,line,' '))
        {
            if(line.compare("<") == 0)
            {
                iss>>to;
                if(score<to)
                    cout<<"correct."<<endl;
                else
                    cout<<"incorrect."<<endl;
                break;

            }
            else if(line.compare("<=") == 0)
            {
                iss>>to;
                if(score<=to)
                    cout<<"correct."<<endl;
                else
                    cout<<"incorrect."<<endl;
                break;
            }
            else if(line.compare(">") == 0)
            {
                iss>>to;
                if(score>to)
                    cout<<"correct."<<endl;
                else
                    cout<<"incorrect."<<endl;
                break;
            }
            else if(line.compare(">=") == 0)
            {
                iss>>to;
                if(score>=to)
                    cout<<"correct."<<endl;
                else
                    cout<<"incorrect."<<endl;
                break;
            }
            else if(line.compare("=") == 0)
            {
                iss>>to;
                if(score==to)
                    cout<<"correct."<<endl;
                else
                    cout<<"incorrect."<<endl;
                break;
            }
            else if(line.compare("+") != 0)
            {
                score += parties.find(line)->second;
            }
        }
    }

    return 0;

}

Re: 11629 - Ballot evaluation

Posted: Tue May 01, 2012 11:14 am
by Achilies_Saiful_Buet
Getting WA!!! in this problem.help plz..!!!!

Code: Select all

ACCEPTED

Re: 11629 - Ballot evaluation

Posted: Wed May 02, 2012 1:29 am
by brianfry713
Be careful with the comparison of floating point values, because some values in the input (like 0.1) do not have an exact representation as a floating point number.

One way to prevent precision errors in this problem is to store everything as an integer, don't use any doubles or floats.

Re: 11629 - Ballot evaluation

Posted: Wed May 02, 2012 12:28 pm
by Achilies_Saiful_Buet
Thnx brianfry :) u r really a great helper

Re: 11629 - Ballot evaluation

Posted: Wed Oct 16, 2013 11:24 pm
by brianfry713
The achieved vote percentage of each party always has one digit after the decimal point.
One way you could read the percentage x is using:
int x, i, f;
scanf("%d.%d", &i, &f);
x = i * 10 + f;

Now for the comparisons multiply n * 10.

Re: 11629 - Ballot evaluation

Posted: Mon Jan 13, 2014 2:53 pm
by uDebug
The achieved vote percentage of each party always has one digit after the decimal point.
One way you could read the percentage x is using:
int x, i, f;
scanf("%d.%d", &i, &f);
x = i * 10 + f;

Now for the comparisons multiply n * 10.
Wow! This is super useful. Thanks so much for sharing.

Although I read the warning in the problem, I didn't quite follow why exactly some numbers like 0.1 don't have an exact floating point representation.

Re: 11629 - Ballot evaluation

Posted: Sun Mar 09, 2014 3:19 am
by vsha041
I wasted one hour just to trying to get answers right. Then I compiled my code with GCC and got a warning saying == should not be used with float. So I decided to work with integers instead but it took me one hour to realize that we can just multiply all numbers by 10 before storing them as there is only 1 decimal digit. And then whatever number is in RHS in guesses, multiply that by ten as well. So this problem becomes trivial if you do that. I hope next time I don't spend 1 hour trying to figure this out.

Re: 11629 - Ballot evaluation

Posted: Fri Jan 30, 2015 2:42 pm
by longted
wrong answer
can tell me where is wrong,

Code: Select all

// uva_11629.cpp : ??????????????
//

#include<stdio.h>
#include<string>
#include<string.h>
#include<map>

using namespace std;

bool check(double sum,char *name,double v)
{
	bool flag=false;
	if(strcmp(name,"<")==0)
	{
		if(sum<v)
			flag=true;
	}
	else if(strcmp(name,">")==0)
	{
		if(sum>v)
			flag=true;
	}
	else if(strcmp(name,"<=")==0)
	{
		if(sum<=v)
			flag=true;
	}
	else if(strcmp(name,">=")==0)
	{
		if(sum>=v)
			flag=true;
	}
	else if(strcmp(name,"=")==0)
	{
		if(sum==v)
			flag=true;
	}	
	return flag;
}
bool check_symbol(char *name)
{
	bool flag=false;
	if(strcmp(name,"<")==0)
			flag=true;
	else if(strcmp(name,">")==0)
			flag=true;
	else if(strcmp(name,"<=")==0)
			flag=true;
	else if(strcmp(name,">=")==0)
			flag=true;
	else if(strcmp(name,"=")==0)
			flag=true;

	return flag;
}

int main()
{
	int data_num=0,question=0;
	scanf("%d %d",&data_num,&question);
	map<string,double> my_map;
	for(int i=1;i<=data_num;i++)
	{
		char p_name[256]={NULL};
		double value;
		scanf("%s %lf",p_name,&value);
		my_map[p_name]=value;	
	}
	for(int i=1;i<=question;i++)
	{
		double sum=0;
		char name[256]={NULL};
		while(scanf("%s",name)==1)
		{
			if(strcmp(name,"+")==0 || !check_symbol(name))
				sum+=my_map[name];
			else			
			{
				double v=0;
				scanf("%lf",&v);
				if(check(sum,name,v))
					printf("Guess #%d was correct.\n",i);
				else
					printf("Guess #%d was incorrect.\n",i);
				
				break;
			}
			
		
		
		
		}
	
	
	}
	return 0;
}







Re: 11629 - Ballot evaluation

Posted: Fri Jan 30, 2015 11:58 pm
by brianfry713
Try solving it without using floating point.