Problem K 用分治法尋找最大值(第三講)

題目描述
用分治法求一個整數序列中的最大值。

輸入
輸入有多行,第一行是一個整數n,表示後面有n行。從第二行開始的n行,每行包含用空格隔開的多個整數,第一個整數是m,表示本行後面還有m個用空格隔開整數。

輸出
輸出有多行,對應輸入中從第二行開始,每一行中m個整數中的最大值。

樣例輸入
2
5 1 3 5 9 8
6 6 5 4 3 2 1
樣例輸出
9
6
 

 

#include <stdio.h>
int max(int *A,int start, int end)
{
if(start==end)
{
return A[start];
}
else
{
int max1,max2,mid;
mid=(start+end)/2;
max1=max(A,start,mid);
max2=max(A,mid+1,end);
if(max1>max2)
return max1;
else
return max2;
}
}
int main()
{

int max0,min0;
int n,q,a1,i;
scanf("%d",&q);
while(q--){
scanf("%d",&a1);
int a[a1];
for(i=0;i<a1;i++){
	scanf("%d",&a[i]);
}
n=sizeof(a)/sizeof(int);

max0=max(a,0,n-1);
printf("%d\n",max0);
}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章