英文:
Count and print letters by their repetition
问题
以下是你提供的代码部分的翻译结果:
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String word = br.readLine();
int[] f = new int[123];
int maxcont = 0, min = 122, max = 96;
for (int i = 0; i < word.length(); i++) {
int cont = 0;
for (int j = 0; j < word.length(); j++) {
if (word.charAt(i) == word.charAt(j) && word.codePointAt(i) > 96) {
f[word.codePointAt(i)]++;
cont++;
}
}
if (word.codePointAt(i) < min && word.codePointAt(i) > 96)
min = word.codePointAt(i);
if (word.codePointAt(i) > max)
max = word.codePointAt(i);
word = word.replace(word.charAt(i), 'O');
if (maxcont < cont)
maxcont = cont;
}
while (maxcont != 0) {
for (int i = min; i <= max; i++) {
if (f[i] == maxcont) {
for (int j = maxcont; j > 0; j--) {
bw.write((char) i);
bw.flush();
}
}
}
maxcont--;
}
}
请注意,我只对你提供的代码进行了翻译,没有包含任何额外的内容。如果你有关于这段代码的问题或需要进一步的帮助,请随时提问。
英文:
I have this problem to do:
Read a word composed only by small character and print them on screen starting from the most repeated letter from the less, if two letters have the same repetition time print the smallest value first. It has to be done in the smallest time, so they suggested me to use bufferedread and bufferedwrite. EX:
Input word:
intensified
Output word:
iiieenndfst
I tried this first:
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String word = br.readLine();
int[] f = new int[26];
int maxcont = 0;
StringBuilder rez = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
int cont = 0;
for (int j = 0; j < word.length(); j++){
if(word.charAt(i) == word.charAt(j) && word.codePointAt(i) > 96) {
f[word.codePointAt(i)-97]++;
cont++;
}
}
word = word.replace(word.charAt(i), 'O');
if (maxcont < cont)
maxcont = cont;
}
while(maxcont != 0) {
for (int i = 0; i < f.length; i++) {
if (f[i] == maxcont) {
for (int j = maxcont; j > 0; j--) {
rez.appendCodePoint(i+97);
}
}
}
maxcont--;
}
bw.write(String.valueOf(rez));
bw.flush();
}
And it says that it takes too much time, so I tried this:
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String word = br.readLine();
int[] f = new int[123];
int maxcont = 0, min = 122, max = 96;
for (int i = 0; i < word.length(); i++) {
int cont = 0;
for (int j = 0; j < word.length(); j++){
if(word.charAt(i) == word.charAt(j) && word.codePointAt(i) > 96) {
f[word.codePointAt(i)]++;
cont++;
}
}
if (word.codePointAt(i) < min && word.codePointAt(i) > 96)
min = word.codePointAt(i);
if (word.codePointAt(i) > max)
max = word.codePointAt(i);
word = word.replace(word.charAt(i), 'O');
if (maxcont < cont)
maxcont = cont;
}
while(maxcont != 0) {
for (int i = min; i <= max; i++) {
if (f[i] == maxcont) {
for (int j = maxcont; j > 0; j--) {
bw.write((char) i);
bw.flush();
}
}
}
maxcont--;
}
}
and I have the same time problem, so in which way can I improve my code in order to make it faster??
答案1
得分: 1
你的程序逻辑是正确的。程序挂起的原因是因为当要求获取数据时,BufferedReader会在输入缓冲区为空的情况下挂起。你正试图从System.in读取用户输入,但是还没有通过向用户输出一条消息来进行初始化:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
System.out.println("输入要评估的单词:"); // 添加这一行
String word = br.readLine();
英文:
Your program logic is ok. The reason the program is hanging is because BufferedReader will hang when a demand is made for data but the input buffer is empty. You are trying to read user input from System.in but haven't, initialize by writing out a message to the user requesting input:
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
System.out.println("Enter the word to be evaluated:"); //Add this line
String word = br.readLine();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论