gpt4 book ai didi

java - POJ 2136 垂直直方图

转载 作者:行者123 更新时间:2023-11-30 04:10:55 26 4
gpt4 key购买 nike

这是一个 POJ 问题 - http://poj.org/problem?id=2136我意识到这是一个简单的解决方案,但不知何故,在线法官不接受我的解决方案,尽管它似乎对我的示例输入有用。它说“错误的解决方案”。我已确保问题中提到的没有多余的空格和多余的行。有人可以指出我哪里出错了吗?

import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeMap;
public class poj2136 {

public static void main(String[] args)
{
// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);
int counter = 0;
TreeMap<Character,Integer> hm_frequency = new TreeMap<Character,Integer>();
while(counter<4)
{
String line = sc.nextLine();
String words[] = line.split(" ");
for(String word: words)
{
//System.out.println(word);
for(int i=0; i<word.length();i++)
{
char key = word.charAt(i);
if((int)key>64 && (int)key<91)
{
if(hm_frequency.containsKey(key))
{
hm_frequency.put(key,hm_frequency.get(key)+1);
}
else
{
hm_frequency.put(key,1);
}
}
}

}
counter++;
}

while(true)
{
int max_value = maxvalue(hm_frequency);
char ch = maximumchar(hm_frequency, max_value);
if(max_value == 0)
{
break;
}
Iterator iter = hm_frequency.keySet().iterator();
int stop_flag = 0;
while(iter.hasNext() && stop_flag==0)
{
char key = (Character) iter.next();
if (key== ch)
{
stop_flag = 1;
}
if(hm_frequency.get(key)== max_value)
{
hm_frequency.put(key, max_value-1);
if(stop_flag == 0)
System.out.print("* ");
else
System.out.print("*");
}
else
{
if(stop_flag == 0)
System.out.print(" ");
else
System.out.print(" ");
}
}

System.out.println();
}
Iterator iter = hm_frequency.keySet().iterator();
while(iter.hasNext())
{
char key = (Character) iter.next();
if(iter.hasNext())
System.out.print(key+" ");
else
System.out.print(key);
}
}

public static char maximumchar(TreeMap<Character,Integer> hm_frequency, int max_value)
{
Iterator iter = hm_frequency.keySet().iterator();
char ch = '.';
while(iter.hasNext())
{
char key = (Character) iter.next();
int value = hm_frequency.get(key);
if(value==max_value)
{
ch = key;
}
}
return ch;
}

public static int maxvalue(TreeMap<Character,Integer> hm_frequency)
{
Iterator iter = hm_frequency.keySet().iterator();
int max = 0;
while(iter.hasNext())
{
char key = (Character) iter.next();
int value = hm_frequency.get(key);
if(value>max)
{
max = value;
}
}
return max;
}

}

最佳答案

您需要输出完整的字母表,并且您只打印文本中的字母:

with the upper-case alphabet separated by spaces

关于java - POJ 2136 垂直直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19622183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com