结合数组和递归的C程序 [英] C program that combines arrays and recursion

查看:49
本文介绍了结合数组和递归的C程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于给定数字'n',我们在表格周围放置1到n的数字,并开始删除从2开始的数字,如下所示:

=========== =示例===========

n = 6

1 2 3 4 5 6 - >我们删除了2,4,6

现在我们有:1 _ 3 _ 5 _

然后如果我们想象它们在6之后我们必须删除2但是因为它已删除我们删除2之后的数字是3.因为他们在3之后我们要删除1(5之后的数字)。最后我们得到5号就是答案



我尝试了什么:



i不能将这个过程转换成代码,特别是在数组末尾我们必须从数组的开头开始的部分,特别是递归函数

for a given number 'n' we put numbers form 1 to n around a table and start deleting the numbers starting from 2 like this:
============EXAMPLE===========
n=6
1 2 3 4 5 6 --> we delete 2,4,6
now we have: 1 _ 3 _ 5 _
then if we imagine them around a table after 6 we gotta delete 2 but since it has been deleted we delete the number after 2 which is 3. since they're around a table after 3 we gotta delete 1 (the number after 5). at the end we get to the number 5 which is the answer

What I have tried:

i cant translate this process into code especially the part that at the end of the array we gotta start from the beginning of the array especially the recursive function

推荐答案

我们不会为你做功课。像家庭作业这样的练习将把课程中的知识融入你的大脑,帮助练习新技能并让你的导师知道你需要额外帮助的地方。



一试。它并不像你想象的那么难。然后,如果你仍然卡住,请回来发布你的代码,解释你的问题,我们会尽力帮助
We do not do your homework for you. Exercises like homework are set to cement the knowledge from the course into your brain, to help you practice the new skills and to let your tutor know where you need extra help.

Give it a go. It is not as hard as you think. Then, if you are still stuck, come back and post your code, explain your problem and we will try to help


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



拿一些纸和一支铅笔,画自己桌子的照片。添加它周围的数字。现在玩游戏并遵守规则。当你做对了,再做一次,但这一次,把表画成一个字符串,然后从表的末尾回到开头。当它工作时,再做一次,但这一次写下你遵循的规则:你做什么时间和为什么。如果您已经写下来,请再次执行,但这一次,请遵循您刚写下的规则。它有用吗?如果是这样,那些规则基本上就是您的代码,您所要做的就是转换为实际代码。亲自尝试一下,您可能会发现它并不像您想象的那么困难!



如果您遇到特定问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!
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.

Get some paper and a pencil, and draw yourself a picture of the table. Add the numbers around it. Now "play the game" and follow the rules. When you get it right, do it again, but this time, draw the table as a string line, and go from the end of the table back to the start. When that works, do it again, but this time write down the rules you are following: what you did when and why. When you have then written down, do it again, but this time, follow just the rules you just wrote down. Does it work? When it does, those rules are basically your code, all you have to do is translate to to actual code. Try it yourself, you may find it is not as difficult as you think!

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!


你需要访问一些 Learn C教程学习基础知识。你必须了解数组,malloc和free。



你必须实现一些能够完成工作的递归函数。可能是原型

You need to visit some Learn C tutorial to learn the basics. You must learn about arrays, malloc and free.

You must implement some recursive function which does the job. It could be with the prototype
int* shortenArray(const int *array, int cnt);


这篇关于结合数组和递归的C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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