X(N) ;
N £ 30 ;
n .
(n):
1 | 2 | ... | i | ... | n |
i ;
i = i + 1 ;
1 i n i .
xmax1 = max(x1, x2,, xi,, xn);
x1 = xmax1;
xmax2 = max(x2,, xi,, xn);
x2 = xmax2;
xmax i = max(xi,, xn-1, xn);
xi = xmax i;
xmax n-1 = max(xn-1,xn);
xn-1 = xmax n-1;
. , , ( N-1 N < 2 ). ( ). , :
(i = 1);
max n max, ;
(j = i + 1);
xj (xj > xmax);
xmax = xj nmax = j, ;
j = j + 1;
j £ n;
xn max = xi xi = xmax,
i = i + 1;
, , i £ n-1;
i > n-1.
, , .
(. 9.13). .
. 9.13.
. 9.4.
9.4
n | i | j | xmax | xj | xi | xnmax | nmax | |
n | i | j | xmax | x[j] | x[i] | x[nmax] | nmax |
.
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
|
|
#include<conio.h>
main()
{
int x[30], xmax; /* */
int i, j, n, nmax; /* */
char buf[50]; /* */
clrscr();
CharToOem(" ", buf);
printf("\n %s \n",buf); /* */
scanf("%d", &n);
for(i=0; i < n; i++)
{
CharToOem(" x ",buf);
printf("\n %s[%d] \n",buf,i+1); /* */
scanf("%d", &x[i]);
}
CharToOem(" \n\n X \n\n",buf);
printf("%s",buf);
for(i=0;i<n;i++)
printf(" %5d ",x[i]);
for (i = 0; i < n; i++)/* . */
{
xmax = x[i];
nmax = i;
for(j = i+1; j < n; j++) /*. . */
{
if(x[j] > xmax)
{
xmax = x[j];
nmax = j;
}
}
x[nmax] = x[i];
x[i]=xmax;
}
CharToOem(" \n \n",buf);
printf("%s",buf);
for(i=0;i<n;i++) printf(" %5d ",x[i]);
getch();
}
7 ;
1 6 4 8 2 11 4 ;
.
9.7.
.
|
(EditN). (Edit). X - (ListBoxX).
, . .
.
#include<stdio.h>
#include<stdlib.h>
void TVrDlgClient::BNClickedOk()
{
// INSERT>> Your code here.
int x[30], xmax; /* */
int i, j, n, nmax; /* */
char buf[50]; /* */
ListBoxX->ClearList();
EditN->GetText(buf,10);/* */
n=atoi(buf); /* */
for(i=0; i < n; i++)
{
EditX->GetLine(buf,10,i); /* */
x[i]=atoi(buf); /* */
}
for (i = 0; i < n; i++)/* . */
{
xmax = x[i];
nmax = i;
for(j = i+1; j < n; j++) /*. . */
{
if(x[j] > xmax)
{
xmax = x[j];
nmax = j;
}
}
x[nmax] = x[i];
x[i]=xmax;
}
for (i=0; i< n; i++)/* */
{
sprintf(buf," %3d",x[i]); /* */
ListBoxX->AddString(buf); /* */
}
}
7 ;
1 6 4 8 2 11 4 ;
.
9.8.
9.3.1.2.
(9.5) . .
20 . -50 50. . ( 20) .
|
|