如何在C ++中解决这些问题 [英] How can I solve these problem in C++

查看:72
本文介绍了如何在C ++中解决这些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提到了一些例子来解释下面的问题,我已经编写了一个代码来解决前两个练习,是否有更优化的解决方案,请你帮我完成第三个练习



头部或尾部如果抛出无偏硬币,则硬币(尾部)的值与掉落后的另一侧(头部)相同)。由于经常使用掷硬币这一决定,例如这种方法用于决定哪支球队开始足球比赛。你的任务是模拟掷硬币和通过投掷硬币获得的序列分析。在练习中,T代表尾部,H代表头部。例如,5次投掷的序列可以是:TTHTH。创建一个名为Heads_Or_Tails的程序来解决以下练习。该程序的输入是所谓的试验,即一系列投掷的结果。必须从标准输入读取此输入。输入的第一行包含一个整数N,表示投掷的数量。

输入行的第二行包含正好N个字符,每个字符都是H或T.例如,输入可以如下所示。

8

HHHTHHTT

程序的输出应写入标准输出。输出包含3行:输出中的第i行是练习i的解决方案。



练习1



在试验中发生了多少次正好两个头被甩在一起?例如,按照THHHTHTHHTHHHTHH的顺序,它发生了两次,恰好两个头部相继抛出。



练习2



查找仅包含头部的最长子序列的长度,以及仅包含尾部的最长子序列。将两个长度打印为由空格分隔的整数。 (两个数字中的任何一个都可以为零,但不能同时为两个。)



练习3



我们如果以下两个条件同时存在,则应在试验中将一系列头部称为头部系列:



系列中的第一项是第一项试用,或系列第一项之前的项目是尾部;

系列中的最后一项是试验中的最后一项,或者系列最后一项之后的项目是尾部。

找出头部系列最常用的长度。如果有多个最常见的磁头系列长度,打印时间最长。如果试用版中没有磁头,则打印零。



示例



我们提供三个简单示例澄清练习。使用其他更大的示例测试您的解决方案!



示例1



输入:



10

HHTTHHTHHH

输出:



2

3 2

2

例2



输入:



14

HHTTHHTHHHTHHH

输出:



2

3 2

3

例3



输入:



1

H

输出:



0

1 0

1



我的尝试:



  int  main()
{
char TossesSequence [ 50 ];
int N;
int TwoHeadsN = 0 ;
int LongestH = 0 ;
int LongestT = 0 ;
int HeadsC = 0 ;
int TailsC = 0 ;

cout<< 请输入投掷次数:;
cin>> N;
cout<< 请输入投掷顺序的结果:;
cin>> TossesSequence;


for int i = 0 ; i< N; i ++)
{
if (TossesSequence [i] == < span class =code-string>' H'
{
HeadsC ++;
if (HeadsC> LongestH)
LongestH = HeadsC;
TailsC = 0 ;
}
else if (TossesSequence [i] == ' T'
{
TailsC ++;
if (TailsC> LongestT)
LongestT = TailsC;
if (HeadsC == 2
TwoHeadsN ++;
HeadsC = 0 ;
}
else
{
cout<< 输入错误<< ENDL;
返回 - 1 ;
}
}

如果(HeadsC == 2
TwoHeadsN ++;

cout<< TwoHeadsN<< \ n;
cout<<最长的H << \t<<最长T<< ENDL;
return 0 ;
}

解决方案

这是HomeWork,但你做了一些工作。

建议:

- 首先,我会分开解决每个练习的代码。这只是让阅读代码更容易和调试。



练习3

您已经知道序列的最大长度。

你基本上有2个解决方案。

- 你在扫描输入时建立一个数组,保持你看到每个序列长度的次数。

- for每个可能的长度,你扫描输入,你记住最好的答案。


这是功课,所以我不会给你任何代码!

我的方式这样做是将输入字符串预处理为Heads和Tails序列长度列表:我创建​​两个整数数组,一个用于头部,一个用于尾部,并运行输入数据填充它们的长度序列。

所以在你的例子中:

 THHHTHTHHTHHHTHH 

我最终得到两个包含的数组:

 HeadCount:4 2 3 2 
TailCount:1 2 1 1



从那,你的练习都是微不足道的:

E1:HeadCount中有多少2?

E2:HeadCount中最大的数字和TailCount中的最大数字

E3:创建另一个计算序列数的数组HeadCount中的长度。找到最大的数字!



试一试:它很容易做到,而且比你使用的代码更清晰! :笑:


I've mentioned some examples to explain the problem below,I've written a code to solve the first two exercises ,is there more optimal solution, and could you please help me with the the third exercises

Heads or tails If an unbiased coin is tossed, then the value of the coin (Tails) has the same probability of showing after falling as the other side that contains the coat of arms (Heads). Because of this decision by "tossing a coin" is used frequently, for example this method is used to decide which team starts a football match. Your task is the simulation of the tossing of a coin and the analysis of sequences acquired by tossing a coin. In the exercises T stands for tails and H stands for heads. For example a sequence of 5 tosses can be: TTHTH. Create a program named Heads_Or_Tails to solve the following exercises. The input for the program is a so-called trial, i.e. the outcome of a sequence of tosses. This input must be read from the standard input. The first line of the input contains an integer, N , indicating the number of tosses.
The second line of the input line contains exactly N characters, each character is either H or T. For example, the input can be as follows.
8
HHHTHHTT
The output of the program should be written to the standard output. The output contains 3 lines: the ith line in the output is the solution to exercise i.

Exercise 1

How many times did it occur in the trial that exactly two heads were tossed after each other? For example, in the sequence THHHHTTHHTHHHTHH it occurred twice that exactly two heads were thrown after each other.

Exercise 2

Find the length of the longest subsequence that contained only heads, and the longest subsequence that contained only tails. Print both lengths as integer numbers separated by a space. (Any of the two numbers can be zero, but not both.)

Exercise 3

We shall call a sequence of heads in the trial a heads series, if both of the following two conditions hold:

the first item in the series is the first item in the trial, or the item preceding the first item of the series is tails;
the last item in the series is the last item in the trial, or the item following the last item of the series is tails.
Find out which is the most frequent length for heads series. If there are more than one "most frequent" heads series lengths, print the longest. If there are no heads in the trial, print zero.

Examples

We provide three simple examples to clarify the exercises. Test your solution with other, larger examples as well!

Example 1

Input:

10
HHTTHHTHHH
Output:

2
3 2
2
Example 2

Input:

14
HHTTHHTHHHTHHH
Output:

2
3 2
3
Example 3

Input:

1
H
Output:

0
1 0
1

What I have tried:

int main()
{
	char TossesSequence[50];
	int N;
	int TwoHeadsN =0;
	int LongestH = 0;
	int LongestT = 0;
	int HeadsC = 0;
	int TailsC = 0;

	cout << "Please enter Number of tosses: ";
	cin >> N;
	cout << "Please enter outcome of sequence of the tosses: ";
	cin >> TossesSequence;

	
	for (int i = 0; i < N; i++)
	{
		if (TossesSequence[i] == 'H')
		{
			HeadsC++;
			if (HeadsC > LongestH)
				LongestH = HeadsC;
			TailsC = 0;
		}
		else if (TossesSequence[i] == 'T')
		{
			TailsC++;
			if (TailsC > LongestT)
				LongestT = TailsC;
			if (HeadsC == 2)
				TwoHeadsN++;
				HeadsC = 0;
		}
		else
		{
			cout << "wrong input" << endl;
			return -1;
		}
	}

	if (HeadsC == 2)
		TwoHeadsN++;

	cout << TwoHeadsN << "\n";
	cout << LongestH << "\t" << LongestT << endl;
	return 0;
}

解决方案

This is HomeWork, but you have done some work.
Advice:
- In first I would have separated the code solving each exercises. It is just making reading the code easier and debugging too.

exercise 3
You already know the maximum length of sequence.
you have basically 2 solutions.
- you build an array holding the number of times you see each sequence length as you scan the input.
- for each possible length, you scan the input and you memorize the best answer.


This is homework, so I'll give you no code!
The way I'd do it is to "preprocess" the input string into a list of Heads and Tails sequence lengths: I'd create two arrays of integers, one for heads, one for tails, and run through the input data filling them with the lengths of the sequences.
So in your example:

THHHHTTHHTHHHTHH

I'd end up with two arrays containing:

HeadCount: 4 2 3 2
TailCount: 1 2 1 1


From that, your exercises are all trivial:
E1: How many 2's in HeadCount?
E2: Largest number in HeadCount and largest number in TailCount
E3: Create another array which counts the number of sequence lengths in HeadCount. Find the biggest number!

Give it a try: it's pretty easy to do, and it's a lot clearer than the code you are using! :laugh:


这篇关于如何在C ++中解决这些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆