递归创建所有子集而不使用C语言中的数组 [英] Creating all subsets recursively without using array in C language

查看:112
本文介绍了递归创建所有子集而不使用C语言中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们从用户那里得到非负整数n,我们必须打印set的所有子集({1,2,3,...,n})。



例如n = 3我们必须打印:



{1,2,3}

{1,2}

{1,3}

{1}

{2,3}

{2}

{3}

{}

,s是可选的,序列可以不带任何逗号打印。 (如{1 2 3})我必须补充说,子集的顺序必须与示例完全相同。首先表示具有1的子集,然后是具有2和......的子集。必须首先打印最长的子集。



我尝试过:



我在互联网上看到很多代码用阵列解决这个问题,或者使用一个指示我们是否使用数字的位数组。问题在于,在这个问题中,我们不允许使用-any-类型的数组或其他数据结构,如vector等。即使使用字符串之类的数组行为也是完全禁止的。它必须只通过递归来解决。



我们也不允许使用任何高级功能。例如,如果我们用C编写它,我们只允许使用stdio.h或C ++,只有< iostream>是允许的,没有其他库。



我不知道如何在没有任何数组的情况下执行此操作。如何检查它必须打印的数字,同时管理{}。

We get non negative integer number n from user and we must print all subsets of set ({1,2,3,...,n}).

for example for n=3 we must print:

{1 , 2 , 3}
{1 , 2}
{1 , 3}
{1}
{2 , 3}
{2}
{3}
{}
,s are optional and the sequence can be printed without any comma. (like {1 2 3}) I must add that the sequence of subsets must be exactly like the example. Meaning first the subsets that have 1, then subsets that have 2 and .... The longest subset must be printed first.

What I have tried:

I see a lot of codes in the Internet that solve this problem with arrays or using a bit array that indicate whether we use a number or not. The issue is that in this question, we are not allowed to use -any- type of array or other data structures like vector ,etc. Even using the array behaviour of something like string is completely prohibited. It must be solved only with recursion.

We are also not allowed to use any advanced functions. For example if we write it with C, we are allowed just to use stdio.h or for C++, only <iostream> is allowed and no other library.

I don't know how to do this without any arrays. How to check which number it must print and at the sametime, manage the {}.

推荐答案

您必须为输出实际数据的函数编写代码递归调用该函数用于移除一个项目的所有子集。



但是如何允许存储该输入?如果有疑问请问你的老师!!!
You must write code for a function which is outputing the actual data and than recursive call that function for all subsets in which one item is removed.

But how are you allowed to store that input? If in doubt ask your teacher!!!


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



整个想法是让你思考递归及其工作原理 - 解决这个问题是你理解它的重要部分。拿到你可以递交的东西是不行的。

亲自试试,你可能会发现它并不像你想象的那么难!



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

The whole idea is to get you to think about recursion and how it works - and solving this is a big part of you understanding it properly. Getting handed something you can hand in doesn't do that.
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!


引用:

我看到了很多Internet中使用数组或使用指示我们是否使用数字的位数组来解决此问题的代码。问题在于,在这个问题中,我们不允许使用-any-类型的数组或其他数据结构,如vector等。

I see a lot of codes in the Internet that solve this problem with arrays or using a bit array that indicate whether we use a number or not. The issue is that in this question, we are not allowed to use -any- type of array or other data structures like vector ,etc.



因为任何变量也是位数组,在尊重所有约束条件时没有解决方案。


Since any variable is also a bit array, there is not solution while respecting all constraint.

引用:

我不知道如何做到这一点数组。

I don't know how to do this without any arrays.



从使用数组求解开始,它将为您提供一个思考如何摆脱数组以及如何进行递归的起点。至少你会有一个有效的解决方案。



我们不做你的HomeWork。

HomeWork不会测试你的乞讨技巧其他人做你的工作,它会让你思考并帮助你的老师检查你对你所学课程的理解,以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请展示您的代码并解释这个问题,我们可能会提供帮助。


Start by solving with arrays, it will give you a starting point to think on how to get rid of arrays and what to make recursive. At least you will have a working solution.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.


这篇关于递归创建所有子集而不使用C语言中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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