11629 - Ballot evaluation

All about problems in Volume 116. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
amine.hamdaoui
New poster
Posts: 10
Joined: Tue Aug 07, 2007 7:33 pm

11629 - Ballot evaluation

Post 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;

}
Achilies_Saiful_Buet
New poster
Posts: 16
Joined: Wed Mar 28, 2012 7:24 pm
Location: Dhaka,Bangladesh

Re: 11629 - Ballot evaluation

Post by Achilies_Saiful_Buet »

Getting WA!!! in this problem.help plz..!!!!

Code: Select all

ACCEPTED
Last edited by Achilies_Saiful_Buet on Wed May 02, 2012 12:26 pm, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11629 - Ballot evaluation

Post 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.
Check input and AC output for thousands of problems on uDebug!
Achilies_Saiful_Buet
New poster
Posts: 16
Joined: Wed Mar 28, 2012 7:24 pm
Location: Dhaka,Bangladesh

Re: 11629 - Ballot evaluation

Post by Achilies_Saiful_Buet »

Thnx brianfry :) u r really a great helper
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11629 - Ballot evaluation

Post 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.
Check input and AC output for thousands of problems on uDebug!
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 11629 - Ballot evaluation

Post 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.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.
vsha041
New poster
Posts: 35
Joined: Wed Feb 12, 2014 10:04 am

Re: 11629 - Ballot evaluation

Post 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.
longted
New poster
Posts: 6
Joined: Wed Aug 20, 2014 10:39 am

Re: 11629 - Ballot evaluation

Post 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;
}






brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11629 - Ballot evaluation

Post by brianfry713 »

Try solving it without using floating point.
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 116 (11600-11699)”