Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Read a string of integers, read a single integer on another line, sort the first read plastic data according to the distance from the single integer, and output it back.
Read a string of integers, read a single integer on another line, sort the first read plastic data according to the distance from the single integer, and output it back.
# include & ltiostream & gt

Use namespace std

int main()

{

int s,n,a[ 100],b[ 100],temp,temp 2;

Cout & lt& lt "Please enter the number of integers in this string"

CIN & gt; & gtn;

Cout & lt& lt "Please enter various integers"

for(int I = 1; I < = n; i++)

{

CIN & gt; & gta[I];

}

Cout & lt& lt "Please enter an integer to compare"

CIN & gt; & gts;

for(int j = 1; j & lt= n; j++)

{

b[j]= ABS(s-a[j]); //Calculate the absolute difference between integer columns and items.

}

b[0]= a[0]= 0;

temp = 0;

temp 2 = 0;

for(int g = 1; g & lt= n; G++)// Make some judgments.

{

for(int f = 1; f & lt= n-f; F++)// Where to exchange?

{

if(b[f]& lt; =b[f- 1])

{

temp = b[f]; //Use bubble sorting to sort the absolute difference.

b[f]= b[f- 1];

b[f- 1]= temp;

temp 2 = a[f];

a[f]= a[f- 1];

a[f- 1]= temp 2; //Sort the corresponding integer columns to achieve the purpose.

}

}

}

for(int m = 1; m & lt= n; m++)

{

cout & lt& lta[m]& lt; & lt" ";

}

Returns 0;

}

This question is written in vc++6.0. After debugging, it can be run. In the loop of the above problems, the algorithm may be redundant. Please understand the optimization yourself. There are detailed instructions in the program, so the flow chart is not attached.