如何用递归就数组中的最大数

HaiYang2006 2006-10-29 07:23:26
static void Main(string[] args)
{
int []a = {11,3,6,20,5,9};

int ok = test(a);

Console.WriteLine(ok);



}



public static int test(int []num)
{ int x=0;
int log = num.Length;

for(int t=0;t<log;t++){
if(num[t]>x)
{
x=num[t];

}

}

return x;
}

如何改一下?
...全文
139 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
HaiYang2006 2006-10-30
  • 打赏
  • 举报
回复
谢谢各位!
huang_u 2006-10-30
  • 打赏
  • 举报
回复
public static int test(int []num)
{
int log = num.Length;
for(int x=0;x<log-1;x++)
{
for(int t=0;t<log-1;t++)
{
if(num[t]<num[t+1])
num[t]= num[t+1];
}


}
return num[0] ;
}
bobo0124 2006-10-30
  • 打赏
  • 举报
回复
public static int test(int []num,int start)
{
if( start == 0)
{
return num[0];
}
int a= num[start];
int b= test(num,start-1);
return a>=b?a:b;
}
static void Main(string[] args)
{
int []a = {11,3,6,20,5,9};
int ok = test(a,a.length-1);
Console.WriteLine(ok);
}
Apq001 2006-10-30
  • 打赏
  • 举报
回复
不对,再改过,这个没问题了:
public static int test(int []num, int x,int index)
{
if( index >= num.Length )return x;

return test( num, num[index] > x ? num[index] : x, index+1 );
}
Apq001 2006-10-30
  • 打赏
  • 举报
回复
public static int test(int []num, int x,int index)
{
if( index >= num.Length - 1 || x > num[index] )return x;

return test( num, num[index], index+1 );
}
Apq001 2006-10-30
  • 打赏
  • 举报
回复
public static int test(int []num, int x,int index)
{
if( index >= num.Length - 1 || x > num[index] )return x;

return test( num, x, index+1 );
}
HaiYang2006 2006-10-30
  • 打赏
  • 举报
回复
各位 ,帮忙啊!
HaiYang2006 2006-10-29
  • 打赏
  • 举报
回复
各位 ,帮忙啊!

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧