Click here to Skip to main content
15,914,767 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My code for The new president problem on codeforces but it gives me wrong answer on testCase 11.
please tell me a testCase that my code won't solve it correctly, or you can help me with my code.

here is the problem Problem - A - Codeforces[^]

C++
#include<bits/stdc++.h>
using namespace std;
int main( )
{
    //int t;
    //cin>>t;
    //while(t--)
    //{
        int c,v,arr[109][109],d[109]={0};
        int x=0; bool check=false; int a=0,b=0;
        int aa=0,bb=0;
        cin>>c>>v;
        for(int i=0 ; i<v; i++)
        {
            for(int j=0 ; j<c ; j++)
            {
                cin>>arr[i][j];
            }
        }

        for(int j=0 ; j<v ; j++)
        {
            d[arr[j][0]]++;
        }

        for(int j=1 ; j<=c ; j++)
        {
            x=max(d[j],x);
            if(d[j]==x && check==false)
            {a=j; aa=d[j]; check=true;}
            else if(d[j]==x && check==true)
            {b=j; bb=d[j]; check=false;}
        }

        //cout<<aa<<"  "<<bb<< "    " <<a <<"  "<<b<<endl;
        if(aa>bb && aa>floor(.5*v))
            {cout<<a<<" 1"<<endl; return 0;}
        else if(bb>aa && bb>floor(.5*v))
            {cout<<b<<" 1"<<endl; return 0;}
        else
        {
            int r=1;
            while(true)
            {
                 aa=0; bb=0;
                for(int j=0 ; j<v ; j++)
                {
                    if(arr[j][r]==a)
                        aa++;
                    else if(arr[j][r]==b)
                        bb++;
                }
                //cout<<aa<<"  "<<bb<< "    " <<a <<"  "<<b<<endl;
                if(aa>bb)
                    {cout<<a<<" 2"<<endl; return 0;}
                else if(bb>aa)
                    {cout<<b<<" 2"<<endl; return 0;}
                r++;
            }
        }
    //}
    return 0;
}


What I have tried:

i try to solve it but get wrong answer.
Posted
Updated 6-May-16 20:55pm

There are a few issues here: your code is undocumented, uses pointless variable names, and generally is difficult to understand unless you wrote it. Even then, in a couple of months time, you won't find it easy to work on either!
I'd strongly suggest that you stop using one or two character names and start using names which describe what the variable is used for: it may take a second or two longer to type, but it makes your code so much more readable (and most IDE's will speed the process up by "guessing" what variable name you meant from the first character or so).

When you add to that that we have no ideas what your "new president problem" is, or what "testCase 11" involves, and we can't really help you much. We don't even have a clue what you type into it, much less what it is supposed to output!

But - you can help yourself!
Use the debugger: put a breakpoint on the first line of the function and step through your code. Work out what you expect to happen at each stage and check if it does. If it does, move on to the next line. If it doesn't, why not? Look at the content of the variables while the code is running. See what you can find out.
This is a skill (predictably called "debugging") which - like all skills - you only develop and improve by using. And it's a lot better to develop it on a relatively trivial task like this than on a 100,000 line monster... :laugh:
Go on - give it a try!
 
Share this answer
 
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Run your code on debugger line by line, inspect your variables as they are used, check that everything meet your expectations.
You will pretty soon where is the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900