01
要求
从键盘输入一串数字,将其中的最大数、第二大数打印输出
02
程序
#include <stdio.h>
int main()
{
int a[99];
int i=0;
int len=0;
char ch;
printf ("Please input numbers:");
while(ch=getchar()!='\n')
{
scanf ("%d",&a[i]);
i++;
len++;
}
int max1=a[0];
for (i=1;i<len;i++)
{
if (max1<a[i])
{
max1=a[i];
}
}
printf ("Max1 = %d\n",max1);
int max2=a[0];
for (i=0;i<len;i++)
{
if (max1==max2)
{
max2=a[i+1];
}
if (max2<a[i] && a[i]!=max1)
{
max2=a[i];
}
}
printf ("Max2 = %d\n",max2);
return 0;
}
03
结果
Please input numbers:33 44 55 66 66 6 66 55
Max1 = 66
Max2 = 55