查找字符的字符数组的算法 [英] Algorithm to find an array of characters for a characters

查看:72
本文介绍了查找字符的字符数组的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

算法查找字符(user-)的字符数组。



伪代码和C实现算法中的代码。伪代码应该作为多行注释包含在C程序中。



输出应该是一个元素数组。例如,如果字符数组是:



{'a','b','c','b','z','b'}



我们搜索'b',输出应该是一个包含{1,3}的整数数组。



但是,如果我们搜索'c',输出应该是{2,-1}(-1,因为数组中只有一个'c')。如果数组中没有出现字符,则输出应为{-1,-1}。



输出应显示在屏幕上。请不要忘记通知用户。



请计算代码中的操作次数。



我尝试了什么:



我们尝试编写伪代码

algorithm to find an array of characters for a character (user-).

a pseudocode and a code in C implementing algorithm. The pseudocode should be included in the C program as a multiline comment.

The output should be an array of elements. For instance, if the character array is:

{'a', 'b', 'c', 'b', 'z', 'b'}

and we search for 'b', the output should be an integer array containing {1, 3}.

If we search for 'c', however, the output should be {2, -1} (-1 because there is only one 'c' in the array). If a character does not appear in your array, the output should be {-1, -1}.

The output should be displayed on the screen. Please do not forget to inform the user.

Please compute the number of operations in your code.

What I have tried:

we tried by writing the pseudo code

推荐答案

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是像你想的那么难!首先考虑一下你要返回的内容:以及包含两个数字的数组。它何时返回什么值?怎么知道什么时候返回-1作为值?

提示:我首先在所有元素中使用-1预填充返回数组。然后,如果你没有找到,那就准备好了。想想为什么你会在那之后填写每个元素。



如果遇到特定问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think! Start by thinking about what you have to return: and array which contains two numbers. When does it return what values? How would it know when to return -1 as a value?
Hint: I'd start by prefilling the "return array" with -1 in all elements. Then if you don;t find any, it's ready. Think about why you would fill each of the elements after that.

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


-1是找不到的,因为它是数组中的无效索引。因此,查找数组并将位置填充到答案数组中。最好用-1来初始化答案数组的所有元素。



如果数组中的char超过2x怎么办?



使用一些C ++教程学习语言,如访问数组和屏幕输出,如 Learn C ++ 。安装Visual Studio。
The -1 is for not found, because it is an invalid index in array. So lookup the array and fill the position into the answer array. At best you initialize all elements the answer array with -1.

What if the char is more than 2x in the array?

Use some C++ tutorial to learn the language like accessing an array and screen output like Learn C++. Install Visual Studio.


这篇关于查找字符的字符数组的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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