Вы находитесь на странице: 1из 19

11.

: : ,


, ,
.
: , ,
, ,
C++.
, ,
: , ,
, . ,
, : .



.
,
(, ,
, ..).
, , ..
,
, .

( ,
..).

(
,
..).

( , , ..).

, ,
(
, ..).

,
. ,
.
1. ,
[-100; 100).
.
min. ,
( min=x[0] ).
( for (i=1;i<k;i++) ).
min. i -
min, min=x[i].
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <time.h>
//
#define max 100
void gen (int k,int a, int b,int x[max]);
//
void out (int k,int x[max]);
//
int minimum (int k,int x[max]);
//
int _tmain(int argc, _TCHAR* argv[]){
int mas[max],n;
do {
printf("\n n (n<=100):");
scanf ("%d",&n);
}
while (n>max);
gen(n,-100,100,mas);
out(n,mas);
printf ("\n %d", minimum(n,mas));
system("pause");
return 0;
}
//
void gen(int k,int a, int b, int x[max]){
int i;
srand(time(NULL)*1000);
//
for (i=0;i<k;i++){
x[i]= rand()%(b-a)+a;
// [a,b)
}
}
2

//
void out (int k,int x[max]){
int i;
printf("\n %d : \n",k);
for (i=0;i<k;i++)
printf("%-6d",x[i]);
}
//
int minimum (int k,int x[max]) {
int i,min=x[0];
for (i=1;i<k;i++)
if (min>x[i]) min=x[i];
return min;
}
2.
, .
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <time.h>
//
#define max 100
void gen (int k, float x[max]);
//
float sred_arifm (int k, float x[max]);
/* */
int _tmain(int argc, _TCHAR* argv[]){
float mas[max];
int n;
do {
printf("\n n (n<=100):");
scanf ("%d",&n);
}
while (n>max);
gen(n,mas);
printf ("\n %f",
sred_arifm(n,mas));
system("pause");
return 0;
}
//
void gen(int k, float x[max]){
int i;
printf("\n %d : \n",k);
for (i=0;i<k;i++){
printf("x[%d]= ",i);
scanf("%f",&x[i]);
3

}
}
/* */
float sred_arifm (int k, float x[max]) {
int i;
float sum=0.0;
for (i=0;i<k;i++)
sum+=x[i]; //
return sum/k;
}



.
.
3. ,
[-50; 50).
.
.
, ( if
(x[i]<0) x[i]=-x[i] ). :
.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <time.h>
//
#define max 100
void gen (int k, int a, int b,int x[max]);
//
void out (int k, int x[max]);
//
void zamena (int k, int x[max]);
//
int _tmain(int argc, _TCHAR* argv[]){
int mas[max];
int n;
do {
printf("\n n (n<=100):");
scanf ("%d",&n);
}
while (n>max);
gen(n,-50,50,mas);
printf(" %d : \n", n);
out(n,mas);
zamena (n,mas);
4

printf("\n
:\n");
out(n,mas);
system("pause");
return 0;
}
//
void gen(int k,int a, int b, int x[max]){
int i;
srand(time(NULL)*1000);
for (i=0;i<k;i++){
x[i]=rand()%(b-a)+a;
}
}
//
void out (int k,int x[max]){
int i;
for (i=0;i<k;i++)
printf("%-6d",x[i]);
}
//
void zamena(int k,int x[max]){
int i;
for (i=0;i<k;i++)
if (x[i]<0) x[i]=-x[i];
}



.
4. ,
[-10; 10).
. : x[0] -> x[1], x[1]
-> x[2], ... , x[k-1] -> x[0].

. ( buf=x[k1] ). ( x[i]=x[i-1] ).
:
, ..
,
.
: .
/* */
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <time.h>
//
5

#define max 100


void gen (int k, int a, int b,int x[max]);
//
void out (int k, int x[max]);
//
void sdvig (int k, int x[max]);
//
int _tmain(int argc, _TCHAR* argv[]){
int mas[max];
int n;
do {
printf("\n n (n<=100):");
scanf ("%d",&n);
}
while (n>max);
gen(n,-10,10,mas);
printf(" %d : \n",n);
out(n,mas);
sdvig (n,mas);
printf("\n
: \n");
out(n,mas);
system("pause");
return 0;
}
//
void gen(int k,int a, int b, int x[max]){
int i;
srand(time(NULL)*1000);
for (i=0;i<k;i++){
x[i]=rand()%(b-a)+a;
}
}
//
void out (int k,int x[max]){
int i;
for (i=0;i<k;i++)
printf("%d ",x[i]);
}
//
void sdvig(int k,int x[max]){
int i,buf;
buf=x[k-1];
for (i=k-1;i>0;i--)
x[i]=x[i-1];
x[0]=buf;
}
6



.

.

.
,


.
,
.


1.
.
2. .
.
3.
.
4.
. .
5.

11. : ,

: , ,
, ,
C++.

++, (
), .
.
(,
) .
, .

++
.

.
11.

.
.
1. N , .
: ,
.
2. N ,
[a; b). , 3,
.
3. N ( N ),
[a; b).
.
4. N ,
[a; b).
.
5. N ,
[a; b). n -
k .

.
,
++.
, ,
.
.
:
, ;
;
, ;
;
++;
;
;
.

.
.
.
.
. , ,
8

, , ,
( ),
.
.
.
.
.
, , .
( 19.003-80 19.002-80).
.
++, MS Visual Studio 2010.
.
.
.
.


1. ?
2.
?
3. ?
4.
?
5.
() ?

12. : :

,
,
.
: ,

C++.
, .
. , ,
,
.

.
,
.
9

,
.
.
.

.

. , .
, ,
. ,
, .
.
, , qsort(),
++.
. ,
, ,
.
qsort()
. -, , qsort(),
. ,
, . -,
qsort() ,
, ,
, - . -,
, qsort(),
.

.
.
.
.
, ,
. ,
; , .
,
.
.
, .
,
.
.
,
,
, .
.
10

.
, n*n , n
, n*ln(n) .
, ..
. ,
,
.
:
"" ( );
( );
(-, , ).

"" ( )
(bubble sort,
).
.
"
", ,
. ,
, . , ,
,
, .
.
,
, . ,
, ,
. , ,
"" (. 12.1).

11

. 12.1. ""
// ""
void BubbleSort (int k,int x[max]) {
int i,j,buf;
for (i=k-1;i>0;i--)
for (j=0;j<i;j++)
if (x[j]>x[j+1]) {
buf=x[j];
x[j]=x[j+1];
x[j+1]=buf;
}
}
,
for ,
. ,

, n .
, n-1 ,
n/2 .

12

:
"" ,
.
, ,
. , ,
, .
- (shaker sort
, , ),
, , .
-.
// -
void Shaker(int k,int x[max]){
int i,t;
bool exchange;
do {
exchange = false;
for(i=k-1; i > 0; --i) {
if(x[i-1] > x[i]) {
t = x[i-1];
x[i-1] = x[i];
x[i] = t;
exchange = true;
}
}
for(i=1; i < k; ++i) {
if(x[i-1] > x[i]) {
t = x[i-1];
x[i-1] = x[i];
x[i] = t;
exchange = true;
}
}
} while(exchange);
// ,
}
-
, - N2. ,
,
.

( )
.
.
n - 1
, .. (. 12.2)
:
1. ;
2.
;
13

3. ,
.

. 12.2.
//
void SelectionSort (int k,int x[max]) {
int i,j,min,temp;
for (i=0;i<k-1;i++) {
//
min=i;
//
for (j=i+1;j<k;j++){
if (x[j]<x[min])
min=j;
//
}
temp=x[i];
x[i]=x[min];
x[min]=temp;
}
}
, n-1 ,
n/2 . ,
14

. , n2, -
. ,

, ,
.

(-, ,
)
, (
), :
;
,
;
, ;
( ,
);
;
, .

,
.
; (.
12.3).

15

. 12.3.
//
void InsertSort (int k,int x[max]) {
int i,j, temp;
for (i=0;i<k;i++) {
// , i -
temp=x[i];
//
for (j=i-1; j>=0 && x[j]>temp; j--)
x[j+1]=x[j];// ,
// ,
x[j+1]=temp;
}
}
,
.
, n-1 ;
n2.


, .
16


.
""
.


.

.


1. .
2. ,
.
3. :
, ,
.
4. : "",
, .
5. .

12.

: ,

C++.

++, (
), .

.
,
.
++
.

.
12.

.
.
17

1. ""
, [-100; 100).
.
2.
, [0; 50).
.
3.
, .
.
4. 2m + 1, m , .
. ,
: , ,
.
5. m, m , .
. ,
.

.
,
++.
, .

.
:
, ;
;
, ;
;
++;
;
;
.

.
.
.
.
. , ,
, , ,
( ),
.
.
.
.
18

.
, , .
( 19.003-80 19.002-80).
.
++, MS Visual Studio 2010.
.
.
.
.


1. ?
2. ,
?
3. ()
()?
4.
?
5. -
?

19

Вам также может понравиться