帮我编辑代码:查找子集 [英] Help me edit my code :Find subset
本文介绍了帮我编辑代码:查找子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I'm writing code algorithm "Find subset":
Example: Input: ABC
Ouput: A, B, C, AB, AC, BC, ABC
I have code of this nature but the result is not correct, people watch and then edit it for me.
<pre lang="C#">public string[] TapCon(string s)<br />
{<br />
int max = 1;<br />
int len = 0;<br />
string[] TapCon = null;<br />
foreach (char c in s.ToCharArray())<br />
{<br />
if (c != ''\0'')<br />
{<br />
max *= 2;<br />
len++;<br />
}<br />
}<br />
<br />
<br />
int k = 1;<br />
for (int i = 1; i < max; i++)<br />
{<br />
for (int j = 0; j <= len; i++)<br />
{<br />
if ((k & i) == k)<br />
{<br />
Insert(ref TapCon, s[j].ToString());<br />
}<br />
k <<= 1;<br />
}<br />
}<br />
return TapCon;<br />
<br />
}</pre><br />
推荐答案
第一件事:这是您的作业,因此我们不会为您编辑"-您应该自己做,否则你不会学.
其次,您在第一个循环中的条件语句是多余的:C#字符串没有终止的空字符.
第三,您没有为TapCon分配任何元素,因此在尝试使用它时总会得到一个异常.
First things first: It''s your homework, so we won''t "edit it for you" - you should do it yourself, or you won''t learn.
Secondly, your condition statement in the first loop is redundant: C# strings do not have a terminating null character.
Thirdly, you do not assign any elements to TapCon, so you will always get an exception when you try to use it.
您所追求的被称为 ^ ]的数学术语.我最近有一个类似问题的解决方案.请参阅此处:使用approri算法的数据挖掘(带有数据库) [ ^ ].以下是stackoverflow的链接: http://stackoverflow.com/questions/343654/c-data-structure-to-mimic-cs-listlistint [ ^ ].
干杯!
--MRB
What you are after is called a powerset[^] in mathematical terms. I recently had a solution to a similar question. See here: Data Mining using approri Algorithm (with database)[^]. Here a link to stackoverflow: http://stackoverflow.com/questions/343654/c-data-structure-to-mimic-cs-listlistint[^].
Cheers!
--MRB
问题本身已经包含错误:
输出:A,B,C,AB,AC,BC,ABC".
这里缺少对您理解数学的主要和最重要的部分:空子集,它是任何给定集合的子集.
—SA
The question itself already contains a mistake:
"Ouput: A, B, C, AB, AC, BC, ABC".
Main and most important for your understanding of mathematics is missing here: the empty subset, which is a subset of any given set.
—SA
这篇关于帮我编辑代码:查找子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文