我的“新总统”守则代码强制中的问题不起作用。 [英] My Code for "The New President" problem in code forces doesn't work.
本文介绍了我的“新总统”守则代码强制中的问题不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解决此问题 http://codeforces.com/gym/100155/problem/A [ ^ ]
但它在testCase 35上给出了错误的答案。
通过告诉我一个testCase我的代码无法正确解决它或者告诉我在写作时是否犯了错误来帮助我我的代码。
I am trying to solve this problem http://codeforces.com/gym/100155/problem/A[^]
But it gives me wrong answer at testCase 35.
Help me by telling me a testCase that my code will not solve it correctly or tell me if I did a mistake while writing my code.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Cand {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int c=sc.nextInt();
int v=sc.nextInt();
if(c==1 && v==0)
{
System.out.println(1+" "+1 );
return;
}
HashMap<integer,integer> hm=new HashMap<integer,integer>();
int [][]arr=new int [v][c];
for (int i = 0; i <v;> for (int j = 0; j <c;> arr[i][j]=sc.nextInt();
}
}
for (int i = 0; i < v; i++) {
if(hm.containsKey(arr[i][0]))hm.replace(arr[i][0],hm.get(arr[i][0])+1);
else hm.put(arr[i][0],1);
}
int firstMax=0;
int secondMax=0;
int firstKey=0;
int secondKey=0;
for (Map.Entry<integer,integer>map:hm.entrySet()) {
if(map.getValue()>v/2)
{
System.out.println(map.getKey()+" "+1);
return;
}
else if(map.getValue()>secondMax &&map.getValue()>firstMax)
{
firstMax=map.getValue();
firstKey=map.getKey();
}
else if(map.getValue()>secondMax)
{
secondMax=map.getValue();
secondKey=map.getKey();
}
}
for (int i = 0; i < v; i++) {
for (int j = 1; j <c mode="hold" /> if(arr[i][0]==firstKey || arr[i][0]==secondKey)break;
else if(arr[i][j]==firstKey)
{
firstMax++;
break;
}
else if(arr[i][j]==secondKey)
{
secondMax++;
break;
}
}
}
if(firstMax>secondMax){
System.out.println(firstKey+" "+2);
}
else System.out.println(secondKey+" "+2);
}
}
推荐答案
您的代码行为不符合您的预期,而您不明白为什么!
有一个几乎通用的解决方案:逐步在调试器上运行你的代码。
在Visual Studio 2010中进行调试 - 初学者指南 [ ^ ]
这个解决方案的缺点:
- 这是一个DIY,你是跟踪问题并找到根源的人,这导致了解决方案。
这个解决方案的优点:
- 你看到你的代码行为,你把它与你的期望相匹配。
次要效果
- 你会为自己找到虫子感到骄傲。
- 你的技能会改善ve。
你应该很快就会发现什么是错的。
Your code do not behave the way you expect, and you don't understand why !
There is an almost universal solution: Run your code on debugger step by step.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- You see your code behaviour, you match it against your expectations.
secondary effects
- Your will be proud of finding bugs yourself.
- Your skills will improve.
You should find pretty quickly what is wrong.
好吧,它看起来像一个作业,它不能在这里完成。相反,你应该自己识别错误。
开发一种叫做的技能 - 调试&检查你的代码发生了什么,以及它失败的情况。
-KR
Well, it's look like an homework, which can't be done here. Rather you should identify the error by yourself.
Develop a skill called - Debugging & check what's happening with your code, where it is failing the case.
-KR
这篇关于我的“新总统”守则代码强制中的问题不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文