Level: Medium
You are given a sequence of integers as input, terminated by a -1. (That is, the input integers may be positive, negative or 0. A -1 in the input signals the end of the input.)
-1 is not considered as part of the input.
Find the second largest number in the input. You may not use arrays.
program---
#include<stdio.h>
int main()
{
int n,a,b,tmp;
scanf("%d",&n);
a=n;
scanf("%d",&n);
if(n!=-1)
b=n;
if(a<b)
{tmp=a;a=b;b=tmp;}
scanf("%d",&n);
while(n!=-1)
{
if(a>n)
{
if(b<n)
b=n;
}
else
{b=a;a=n;}
scanf("%d",&n);
}
printf("%d",b);
return 0;
}
You are given a sequence of integers as input, terminated by a -1. (That is, the input integers may be positive, negative or 0. A -1 in the input signals the end of the input.)
-1 is not considered as part of the input.
Find the second largest number in the input. You may not use arrays.
program---
#include<stdio.h>
int main()
{
int n,a,b,tmp;
scanf("%d",&n);
a=n;
scanf("%d",&n);
if(n!=-1)
b=n;
if(a<b)
{tmp=a;a=b;b=tmp;}
scanf("%d",&n);
while(n!=-1)
{
if(a>n)
{
if(b<n)
b=n;
}
else
{b=a;a=n;}
scanf("%d",&n);
}
printf("%d",b);
return 0;
}
No comments:
Post a Comment