如何找到抛硬币中出现次数最多的正面长度?
How to find the most frequent length of Heads in a Coin Toss?
我需要找出头像系列最常见的长度。如果有多个 most frequent heads series lengths,打印最长的。如果试验中没有正面朝上,则打印零。
Example :
Input : HTTHH
Output : 2
Input : HTTHHHTTHHH
EDIT : Sorry I forgot to include the Code.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
char[] c = str.toCharArray();
int currentLen = 0;
int frequentLen = 0;
for (int i = 0; i < str.length(); i++)
{
if (c[i] == 'H')
{
currentLen++;
if (currentLen > frequentLen)
{
frequentLen = currentLen;
}
}
else
{
currentLen = 0;
}
}
System.out.println(frequentLen);
当我运行这段代码时,一些输入的输出是不同的。
例如:当我给出 HHTTHHTTHHHTHHH
时它显示 2
但是根据分配它应该显示 3
.
因为如果有不止一个最频繁的长度它应该显示最长的。请帮忙。
您的 else
与第二个 if
而不是第一个关联,因此您的 frequentLen
计数器在错误的时间被清零。这是因为您没有适当的大括号来使编译器清楚地了解关联。您应该养成在 if
语句中始终使用大括号的习惯,即使是单行语句。他们会防止像这样的错误。不要仅仅依靠缩进——它在语法上并不重要。此外,由于您计算的是最大长度而不是最常见的长度,因此更改变量名称也很有意义。
更正后的代码是:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
char[] c = str.toCharArray();
int currentLen = 0;
int maxLen = 0;
for (int i = 0; i < str.length(); i++) {
if (c[i] == 'H') {
currentLen++;
if (currentLen > maxLen) {
maxLen = currentLen;
}
}
else {
currentLen = 0;
}
}
System.out.println(maxLen);
让我们把事情搞清楚。序列的最频繁长度是出现次数最多的长度。所以不知何故,你必须跟踪已经出现的每个长度的序列数。这是执行此操作的代码。
public static void freq() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
// Count the number of times each sequence appeared. The array is indexed by the length of a sequence
int[] counts = new int[str.length()];
int current = 0;
for (char value : str.toCharArray()) {
if (value == 'H') {
current++;
} else if (current != 0){
counts[current]++;
current = 0;
}
}
// If we end with 'H', increment the count
if (current != 0) { counts[current]++; }
// From the computed counts, find the one that appears the most often
int mostFrequentLength = 0;
int mostFrequentValue = 0;
for (int i = 1; i < counts.length; i++) {
if (counts[i] >= mostFrequentValue) {
mostFrequentValue = counts[i];
mostFrequentLength = i;
}
}
System.out.println(mostFrequentLength);
}
现在,这段代码中有两个循环,但如果我们在第一个循环中更新最频繁的序列,我们可以将其合并为一个。另外,您可能想找到一种替代方法来创建与您正在考虑的字符串长度相同的数组。
我需要找出头像系列最常见的长度。如果有多个 most frequent heads series lengths,打印最长的。如果试验中没有正面朝上,则打印零。
Example :
Input : HTTHH
Output : 2
Input : HTTHHHTTHHH
EDIT : Sorry I forgot to include the Code.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
char[] c = str.toCharArray();
int currentLen = 0;
int frequentLen = 0;
for (int i = 0; i < str.length(); i++)
{
if (c[i] == 'H')
{
currentLen++;
if (currentLen > frequentLen)
{
frequentLen = currentLen;
}
}
else
{
currentLen = 0;
}
}
System.out.println(frequentLen);
当我运行这段代码时,一些输入的输出是不同的。
例如:当我给出 HHTTHHTTHHHTHHH
时它显示 2
但是根据分配它应该显示 3
.
因为如果有不止一个最频繁的长度它应该显示最长的。请帮忙。
您的 else
与第二个 if
而不是第一个关联,因此您的 frequentLen
计数器在错误的时间被清零。这是因为您没有适当的大括号来使编译器清楚地了解关联。您应该养成在 if
语句中始终使用大括号的习惯,即使是单行语句。他们会防止像这样的错误。不要仅仅依靠缩进——它在语法上并不重要。此外,由于您计算的是最大长度而不是最常见的长度,因此更改变量名称也很有意义。
更正后的代码是:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
char[] c = str.toCharArray();
int currentLen = 0;
int maxLen = 0;
for (int i = 0; i < str.length(); i++) {
if (c[i] == 'H') {
currentLen++;
if (currentLen > maxLen) {
maxLen = currentLen;
}
}
else {
currentLen = 0;
}
}
System.out.println(maxLen);
让我们把事情搞清楚。序列的最频繁长度是出现次数最多的长度。所以不知何故,你必须跟踪已经出现的每个长度的序列数。这是执行此操作的代码。
public static void freq() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
// Count the number of times each sequence appeared. The array is indexed by the length of a sequence
int[] counts = new int[str.length()];
int current = 0;
for (char value : str.toCharArray()) {
if (value == 'H') {
current++;
} else if (current != 0){
counts[current]++;
current = 0;
}
}
// If we end with 'H', increment the count
if (current != 0) { counts[current]++; }
// From the computed counts, find the one that appears the most often
int mostFrequentLength = 0;
int mostFrequentValue = 0;
for (int i = 1; i < counts.length; i++) {
if (counts[i] >= mostFrequentValue) {
mostFrequentValue = counts[i];
mostFrequentLength = i;
}
}
System.out.println(mostFrequentLength);
}
现在,这段代码中有两个循环,但如果我们在第一个循环中更新最频繁的序列,我们可以将其合并为一个。另外,您可能想找到一种替代方法来创建与您正在考虑的字符串长度相同的数组。