统计并打印字母及其重复次数

huangapple go评论86阅读模式
英文:

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 &lt; word.length(); i++) {
int cont = 0;
for (int j = 0; j &lt; word.length(); j++){
if(word.charAt(i) == word.charAt(j) &amp;&amp; word.codePointAt(i) &gt; 96) {
f[word.codePointAt(i)-97]++;
cont++;
}
}
word = word.replace(word.charAt(i), &#39;O&#39;);
if (maxcont &lt; cont)
maxcont = cont;
}
while(maxcont != 0) {
for (int i = 0; i &lt; f.length; i++) {
if (f[i] == maxcont) {
for (int j = maxcont; j &gt; 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 &lt; word.length(); i++) {
int cont = 0;
for (int j = 0; j &lt; word.length(); j++){
if(word.charAt(i) == word.charAt(j) &amp;&amp; word.codePointAt(i) &gt; 96) {
f[word.codePointAt(i)]++;
cont++;
}
}
if (word.codePointAt(i) &lt; min &amp;&amp; word.codePointAt(i) &gt; 96)
min = word.codePointAt(i);
if (word.codePointAt(i) &gt; max)
max = word.codePointAt(i);
word = word.replace(word.charAt(i), &#39;O&#39;);
if (maxcont &lt; cont)
maxcont = cont;
}
while(maxcont != 0) {
for (int i = min; i &lt;= max; i++) {
if (f[i] == maxcont) {
for (int j = maxcont; j &gt; 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(&quot;Enter the word to be evaluated:&quot;); //Add this line
String word = br.readLine(); 

huangapple
  • 本文由 发表于 2020年5月3日 22:44:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61576346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定