本帖最后由 jackz007 于 2020-12-25 11:07 编辑 #include <stdio.h>
int main(void)
{
int d[256] = {0} , e[256] = {0} , f , i , j , n , t ;
int largest , second_largest ;
for(i = 0 ; i < 256 ; i ++) e[i] = i ;
scanf("%d" , & n) ;
for(i = 0 ; i < n ; i ++) scanf("%d" , & d[i]) ;
for(i = 0 ; i < n - 1 ; i ++) {
for(j = i + 1 ; j < n ; j ++) {
if(d[e[i]] < d[e[j]]) {
t = e[i] ;
e[i] = e[j] ;
e[j] = t ;
}
}
}
for(largest = second_largest = -1 , f = 0 , i = 1 ; i < n ; i ++) {
if(d[e[i]] < d[e[0]]) {
largest = e[0] ;
second_largest = e[i] ;
f ++ ;
break ;
}
}
if(f) {
printf("index : %d , %d\n" , largest , second_largest) ;
printf("value : %d , %d\n" , d[largest] , d[second_largest]) ;
} else {
printf("none of largest and second_largest data.\n") ;
}
}
编译、运行实况:D:\00.Excise\C>g++ -o x x.c
D:\00.Excise\C>x
18
1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1
index : 8 , 7
value : 9 , 8
D:\00.Excise\C>
|