#include
#define SIZE 6
void main( void )
{
int num[SIZE];
int i, Min, MinPos;
printf("Enter 6 integers:");
for(i=0; i scanf("%d",&num[i]); Min=num[0]; MinPos= 0; for(i=1; i if( Min > num[i] ) { Min=num[i]; MinPos=i; } printf("Min value is %d\n", Min); printf("It’s positionis %d\n", MinPos+1); } 注:如果要打印最小数所在位置时,要记得加1,这才是正常位置(c语言数组下标从0开始)。 程序运行结果如下:
int a[6];
int n=6; //数组长度为20
int min=0,i=0;
for(i=0;iscanf("%d",&a[i]);
}
for(i=0;iif(a[i] min=i;
}
}
printf("最小值为:%d\n最小值的下标为:%d\n",a[min],min);
这种是排序问题,有很多解法,给你写几个看看吧
PS:这都是我之前写过的代码,可能不是求最小的,不过原理是一样的
1、快速排序:
void exchange( int* a, int i, int j )
{
int exchange = a[ j ];
a[ j ] = a[ i ];
a[ i ] = exchange;
}
int partition( int* a, int p, int r )
{
int x = a[ r ];
int i = p - 1;
for( int j = p; j < r; j++ )
{
if( a[ j ] <= a[ r ] )
{
i = i + 1;
exchange( a, i, j );
}
}
i = i + 1;
exchange( a, i, r );
return i;
}
void quicksort( int* a, int p, int r )
{
if( p < r )
{
int q = partition( a, p , r );
quicksort( a, p, q - 1 );
quicksort( a, q + 1, r );
}
}
最后在main里面调用quicksort就可以了;
2、合并排序法:
#define INF 0x7FFFFFFF
void merge( int A[], int p, int q, int r )
{
int n1 = q - p + 1;
int n2 = r - q;
int *L = new int[n1+1];
int *R = new int[n2+1];
for( int i = 0; i < n1; i++ )
{
L[ i ] = A[ p+i ];
}
for( int j = 0; j < n2; j++ )
{
R[ j ] = A[ q+j+1 ];
}
L[ n1 ] = INF;
R[ n2 ] = INF;
int i = 0;
int j = 0;
int k = p;
for( ; k < r ; k++ )
{
if( L[ i ] < R[ j ] )
{
A[ k ] = L[ i ];
i++;
}
else
{
A[ k ] = R[ j ];
j++;
}
}
if( L[i] == INF )
A[ r ] = R[ j ];
else
A[ r ] = L[ i ];
}
void merge_sort( int A[], int p, int r )
{
if( p < r )
{
int q = ( p + r )/2;
merge_sort( A, p, q );
merge_sort( A, q+1, r );
merge( A, p, q, r );
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int A[] = {5,2,4,7,1,3,2,6};
merge_sort( A, 0 , 7 );
for( int i = 0; i < 8; i++ )
printf("%d\n",A[ i ]);
printf("\n");
return 0;
}
#include
void main()
{int sr[6]; //定义输入数组
int i,a,min;
printf("请输入6个不同的数\n");
for(i=0;i<=5;i++)
{printf("请输入第%d个数\t",i+1);
scanf("%d",&sr[i]);
}
for(i=0,a=sr[0];i<=5;i++)
{if(sr[i] {a=sr[i];
min=i;
}
}
printf("最小为:%d,对应下标为:%d\n",a,min);
}