当前页面: 开发资料首页 → J2EE 专题 → 如何用递归就数组中的最大数
如何用递归就数组中的最大数
摘要: 如何用递归就数组中的最大数
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
if(num[t]>x)
{
x=num[t];
}
}
return x;
}
如何改一下?
只是取得最大的又不用排序 用递归干嘛。写的可以了
取最大单循环即可,还用什么递归啊?
public static int test(int []num,int i,int max)
{
if(i<=0)
{
return max;
}
if(num[i]>max)
{
max=num[i];
}
return test(num,i-1,max);
}
不知道可不可以,没试
public class FindMax {
static Integer []num = {11,3,6,20,5,9,78,3,68,99};
public FindMax() {
}
public static void main(String[] args) {
System.out.print ("The array is [");
for (Integer i : num)
System.out.print (i+",");
System.out.println ("]. The largest one in this array is "+find(num.length-1));
}
//递归方法
public static int find(int t){
if (t == 0){
return num[t];
}else{
return (num[t]>find(t-1))?num[t]:find(t-1);
}
}
}
大概是这个意思吧