利用递归求n个数中的最大数
发布网友
发布时间:2022-04-18 13:47
我来回答
共6个回答
热心网友
时间:2022-04-18 15:17
#include <stdio.h>
int main()
{
int max(int x, int y);
int Rand_Num[100]={0,1,2,3,4,5,6,7,8,9,10}, Size=10;
int Num;
for(int i=0;i<Size;i++)
Num=max(Rand_Num[i],Rand_Num[i+1]);
printf("Max Num is:%d\n",Num);
return 0;
}
int max(int x, int y)
{
int z;
z = x > y ? x : y;
return z;
}
#include <stdio.h>
int main()
{
int max(int x, int y);
int Rand_Num[100]={0,1,2,3,4,5,6,7,8,9,10}, Size=10;
int Num;
for(int i=0;i<Size;i++)
Num=Rand_Num[i] >Rand_Num[i+1] ? Rand_Num[i] : Rand_Num[i+1];
printf("Max Num is:%d\n",Num);
return 0;
}
热心网友
时间:2022-04-18 16:35
#include<stdio.h>
int findmax(int a[],int n)//递归求最大值
{
if(n==1)return a[0];//递归结束后a[0]为最大值
if(a[0]>=a[n-1])findmax(a,n-1);
else
{
a[0]=a[n-1];//最大值放到a[0]
findmax(a,n-1);
}
}
void main()
{
int a[10]={1,21,34,14,5,23,7,86,92,10};
printf("%d\n",findmax(a,10));
}
热心网友
时间:2022-04-18 18:09
// 递归求a[i]~a[j]中的最大值
int max(int *a,int i, int j)
{
if(i==j)
return a[i];
int temp;
temp=max(a,i+1,j);
return a[i]>temp?a[i]:temp;
}
#include <stdio.h>
int main()
{
int Num[20]={0,1,23,43,74,15,6,37,89,15}, size=10;
int result;
result=max(Num,0,size-1);
printf("Max num is:%d\n",result);
return 0;
}
热心网友
时间:2022-04-18 20:01
二分法,时间效率为O(n)
栈的深度只需要log(n)
#include <stdio.h>
int max(int *data, int start, int end);
int main()
{
int num[10] = { 1, 3, 15, 6, 62, 123, 23, 1, 2 };
printf("The max number is %d\n", max(num, 0, 9));
return 0;
}
int max(int *data, int start, int end)
{
int max1, max2, mid;
if(start == end)
return data[start];
mid = (start + end) / 2;
max1 = max(data, start, mid);
max2 = max(data, mid + 1, end);
return max1 > max2 ? max1 : max2;
}
热心网友
时间:2022-04-18 22:09
#include
<stdio.h>
int
main()
{
int
max(int
x,
int
y);
int
Rand_Num[100]={0,1,2,3,4,5,6,7,8,9,10},
Size=10;
int
Num;
for(int
i=0;i<Size;i++)
Num=max(Rand_Num[i],Rand_Num[i+1]);
printf("Max
Num
is:%d\n",Num);
return
0;
}
int
max(int
x,
int
y)
{
int
z;
z
=
x
>
y
?
x
:
y;
return
z;
}
#include
<stdio.h>
int
main()
{
int
max(int
x,
int
y);
int
Rand_Num[100]={0,1,2,3,4,5,6,7,8,9,10},
Size=10;
int
Num;
for(int
i=0;i<Size;i++)
Num=Rand_Num[i]
>Rand_Num[i+1]
?
Rand_Num[i]
:
Rand_Num[i+1];
printf("Max
Num
is:%d\n",Num);
return
0;
}
热心网友
时间:2022-04-19 00:33
直接能够运行!
#include "stdio.h"
void main()
{
int i, j, min, temp, N;
int a[1000];
printf("请输入排序的个数: ");
scanf("%d",&N);
printf("\n请输入这%d个数字:\n",N);
for(i=1; i<=N; i++)
scanf("%d",&a[i]);
printf("\n");
for(i=1; i<=N; i++)
{
min = i;
for (j=i+1; j<=N; j++)
if (a[min]>a[j])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
printf("由小到大顺序是:\n");
for(i=1; i<=N; i++)
printf("%d\n",a[i]);
printf("\n");
}