我正在处理这个codeHS AP CompSci问题(4.3.6)
基本上我必须模拟翻转硬币100次,打印出结果(头部或尾部),但最后我需要打印出最长的条纹。
public class LongestStreak extends ConsoleProgram
{
public static final int FLIPS = 100;
boolean tailsChosen = true;
int count_tails=0;
int count_heads=0;
int streak = 1;
public void run()
{
for (int i= 1; i<=100; i++)
{
tailsChosen= Randomizer.nextBoolean();
if (tailsChosen)
{
System.out.println("Tails");
count_tails++;
}
else
{
System.out.println("Heads");
count_heads++;
}
}
System.out.println("Longest streak of heads: " + streak );
}
}
到目前为止,这是我的编码。我已经工作过,我可以随机打印100个头部和尾部,但却找不到找到最长条纹的方法..
谢谢
答案 0 :(得分:1)
如果它是一个头,你可以增加条纹,如果它是一个尾,你可以将它重置为0。然后将它与之前的maxHeadStreak进行比较。
int streak = 0; // should be init to zero
// other codes
for (int i= 1; i<=FLIPS; i++) // use your constant
{
tailsChosen= Randomizer.nextBoolean();
if (tailsChosen)
{
System.out.println("Tails");
count_tails++;
streak = 0;
}
else
{
System.out.println("Heads");
count_heads++;
streak++;
maxHeadStreak = Math.max(streak, maxHeadStreak);
}
}
答案 1 :(得分:1)
我建议使用max_head_streak
变量来存储到目前为止实现的最大头部条纹。每次翻转头时,count_heads
变量都会递增。
一旦翻转尾部且头部条纹断裂,检查当前头条纹是否高于最大头条纹以及是否将max_head_streak
设置为当前count_heads
。然后将当前头数count_heads
设置回0。
翻转100个硬币后打印max_head_streak
。
public static final int FLIPS = 100;
boolean tailsChosen = true;
int max_head_streak=0;
int count_heads=0;
public void run()
{
for (int i= 1; i<=FLIPS; i++)
{
tailsChosen= Randomizer.nextBoolean();
if (tailsChosen)
{
System.out.println("Tails");
if (max_head_streak < count_heads)
{
max_head_streak = count_heads;
{
count_heads = 0;
}
else
{
System.out.println("Heads");
count_heads++;
}
}
System.out.println("Longest streak of heads: " + max_head_streak);
}
一些评论:
您可以省略tails_count
,因为您不需要计算尾巴。
您可以在开头定义翻转次数,但不要使用它
只需将FLIPS
放入循环声明中。
答案 2 :(得分:1)
public class LongestStreak extends ConsoleProgram
{
public static final int FLIPS = 100;
public void run()
{
int headStreak = 0;
int longestHeadStreak = 0;
for (int i = 0; i < 10; i++)
{
if (Randomizer.nextBoolean())
{
System.out.println("Heads");
headStreak++;
}
else
{
System.out.println("Tails");
if (longestHeadStreak < headStreak)
{
longestHeadStreak = headStreak;
}
headStreak = 0;
}
}
System.out.println("Longest streak of heads: " + longestHeadStreak);
}
}
答案 3 :(得分:0)
这样的事情应该做。
基本上,跟踪当前条纹的长度(你清楚地意识到这一点),然后跟踪它的最大值。
public class LongestStreak {
public static final int FLIPS = 100;
Random random = new Random();
boolean lastFlip = true;
int count_tails = 0;
int count_heads = 0;
int streak = 0;
int longestTailsStreak = 0;
int longestHeadsStreak = 0;
public void run() {
for (int i = 1; i <= FLIPS; i++) {
// Flip the coin.
boolean itsTails = random.nextBoolean();
// Keep track of the length of the current streak.
if (itsTails == lastFlip) {
// The streak continues.
streak += 1;
} else {
// End of streak.
if (itsTails) {
// It must have been a run of heads.
longestHeadsStreak = Math.max(longestHeadsStreak, streak);
} else {
// It must have been a run of tails.
longestTailsStreak = Math.max(longestTailsStreak, streak);
}
streak = 0;
}
// Count them.
if (itsTails) {
System.out.println("Tails");
count_tails += 1;
} else {
System.out.println("Heads");
count_heads += 1;
}
lastFlip = itsTails;
}
System.out.println("Longest streak of heads: " + longestHeadsStreak);
System.out.println("Longest streak of tails: " + longestTailsStreak);
}
}