如何扭曲递归函数 [英] How to Wrire Recurssive Function
问题描述
亲爱的朋友们,
这里我想写Recurssive函数的格式是这样的
UserID,UnitName,P.ID
1 ANIL 0
2 KUMAR 1
3 Phani 1
4 Shruthi 2
如果我搜索P.ID = 1数据应该过滤像
UserID,UnitName,P.ID
2 KUMAR 1
3 Phani 1
喜欢这个但我搜索谷歌和我发现,我做了更改仍然我失败
<前lang =c#> 静态 void Main( string [] args)
{
List< Array> myList = new List< Array>();
myList.Add( new string [ 0 跨度>]);
myList.Add( new string [ 0 跨度>]);
myList.Add( new string [ 0 跨度>]);
myList [ 0 ] = new string [] { 1, 2, 3, 4};
myList [ 1 ] = new string [] { Anil, Kumar, Phani, Shruthi};
myList [ 2 ] = new string [] { 0, 1, 1, 2};
foreach ( string x in foo( 0 ,myList))
{
Console.WriteLine(x);
}
Console.ReadKey();
静态列表< string> foo( int a,List< Array> x)
{
List< string> retval = new List< string>();
if (a == x.Count)
{
retval.Add( );
return retval;
}
foreach (对象 y in x [a])
{
foreach ( string x2 foo(a + 1 ,x))
{
retval .Add(y.ToString()+ + x2.ToString());
}
}
return retval;
}
请有人解决此问题。
问候,
AnilKumar.D 你好了我的朋友。首先,你永远不会得到你想要的P.ID。其次,你只是简单地连接3个数组的内容来获得你的输出...不要让它变得比它更复杂。第三,没有理由进行递归功能。
静态 void Main( string [] args)
{
List< string> userID = new 列表< string>()= { 1, 2, 3, 4跨度>};
List< string> unitName = new 列表< string>()= { Anil, Kumar, Phani, Shruthi跨度>};
List< string> pID = new 列表< string>()= { 0, 1, 1, 2跨度>};
for ( int i = 0 ; i < pID.Count; i ++)
{
if (pID [i] == args [ 0 ])
{
Console.WriteLine(userID [i] + + unitName [i] + < span class =code-string> + pID [i]);
}
}
Console.ReadKey();
调用你的程序在命令行上传递所需的P.ID。您将获得所需的输出。
Dear Friends,
Here i want to Write Recurssive Function the Format is Like This
UserID , UnitName , P.ID
1 ANIL 0
2 KUMAR 1
3 Phani 1
4 Shruthi 2
If i Search Through P.ID =1 The data Should Filter like
UserID , UnitName , P.ID
2 KUMAR 1
3 Phani 1
Like this But i searched Google and i Found and i made changes still iam failed
static void Main(string[] args)
{
List<Array> myList = new List<Array>();
myList.Add(new string[0]);
myList.Add(new string[0]);
myList.Add(new string[0]);
myList[0] = new string[] { "1", "2","3","4"};
myList[1] = new string[] { "Anil", "Kumar","Phani","Shruthi"};
myList[2] = new string[] { "0", "1" ,"1","2"};
foreach (string x in foo(0, myList))
{
Console.WriteLine(x);
}
Console.ReadKey();
static List<string> foo(int a, List<Array> x)
{
List<string> retval = new List<string>();
if (a == x.Count)
{
retval.Add("");
return retval;
}
foreach (Object y in x[a])
{
foreach (string x2 in foo(a + 1, x))
{
retval.Add(y.ToString() + " " + x2.ToString());
}
}
return retval;
}
Please anybody can solve this.
Regards,
AnilKumar.D
You are WAY off my friend. First, you don't ever get the P.ID you are looking for. Second, you are simply concatenating the contents of 3 arrays to get your output... don't make it more complicated than it has to be. Thirdly, there is no reason for a recursive function.
static void Main(string[] args) { List<string> userID = new List<string>() = { "1", "2","3","4"}; List<string> unitName = new List<string>() = { "Anil", "Kumar","Phani","Shruthi"}; List<string> pID = new List<string>() = { "0", "1" ,"1","2"}; for(int i=0; i < pID.Count; i++) { if (pID[i] == args[0]) { Console.WriteLine(userID[i] + " " + unitName[i] + " " + pID[i]); } } Console.ReadKey();
Call your program passing the desired P.ID on the command line. You will get the output you want.
这篇关于如何扭曲递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!