我的“新总统”代码代码强制问题得到WA [英] My code for "the new president" problem on code forces get WA

查看:89
本文介绍了我的“新总统”代码代码强制问题得到WA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于代码强制新总统问题的代码,但它给了我关于testCase 11的错误答案。

请告诉我一个testCase,我的代码无法正确解决,或者你可以帮助我用我的代码。



这里有问题问题 - A - Codeforces [ ^ ]



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[^]

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





我的尝试:



i尝试解决它但得到错误的答案。



What I have tried:

i try to solve it but get wrong answer.

推荐答案

这里有一些问题:你的代码没有文档,使用无意义的变量名,以及除非你写的,否则一般很难理解。即使这样,在几个月的时间内,你也不会觉得它很容易处理!

我强烈建议你停止使用一个或两个字符名称并开始使用描述的名字该变量用于什么:键入可能需要一两秒钟,但它会使您的代码更具可读性(并且大多数IDE会通过猜测您从第一个字符中表示的变量名称来加快处理速度或所以)。



当你加入时,我们不知道你的新总统问题是什么,或者testCase 11涉及什么,我们可以'真的对你有所帮助。我们甚至不知道你输入的是什么,更不用说它应该输出什么了!



但是 - 你可以帮助自己!

使用调试器:在函数的第一行放置一个断点并逐步执行代码。找出你期望在每个阶段发生的事情并检查它是否确实发生。如果是,请转到下一行。如果没有,为什么不呢?在代码运行时查看变量的内容。看看你能找到什么。

这是一项技能(可预见地称为调试) - 就像所有技能一样 - 你只能通过使用来发展和提高。在这样一个相对琐碎的任务上开发它比在10万线怪物上开发它要好得多......:笑:

继续 - 尝试一下!
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!

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



逐行在调试器上运行代码,检查变量使用情况,检查一切是否符合您的要求期待。

你很快就会遇到问题。
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.


这篇关于我的“新总统”代码代码强制问题得到WA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆