题目描述
求n个整数中的最大的两个元素。要求定义一个函数LargestTow(),求数组a的最大的两个元素,分别存入形参指针pfirst和psecond所指存储单元,函数原型如下:
void LargestTow(int a[],int n,int *pfirst,int *psecond)
{
/*数组a有n个元素,将数组中的最大值存入形参指针pfirst所指内存单元,将数组中第二大的值存入形参指针psecond所指内存单元。 */
}
输入
输入有两行,输入第一行是一个整数n,1<n<=1000;第二行是n个整数,由空格隔开。
输出
输出两个整数,表示数组中最大的两个值。输出占一行。
样例输入 Copy
5
6 3 4 9 8
样例输出 Copy
9 8
#include<stdio.h>
void LargestTow(int a[],int n,int *pfirst,int *psecond);
int main()
{
int a[1000], n, i;
int *pfirst=NULL, *psecond=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
LargestTow(a, n, pfirst, psecond);
return 0;
}
void LargestTow(int a[],int n,int *pfirst,int *psecond)
{
int i, j, t;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
pfirst=&a[n-1];
psecond=&a[n-2];
printf("%d %d\n",*pfirst, *psecond);
}