| |

VerySource

 Forgot password?
 Register
Search
Author: myjinx

C ++ masters help, give an answer

[Copy link]

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-6-11 13:00:01
| Show all posts
Record the position of the maximum and minimum values, and then exchange with the head and tail
How to write only the largest and smallest?
Big brother upstairs, thank you in advance.
Reply

Use magic Report

0

Threads

8

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-12 10:00:01
| Show all posts
No bubble sorting is required. Just traverse twice, find the largest one the first time, swap with the first element, find the smallest the second time, and swap the last element,
Reply

Use magic Report

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-6-12 13:30:01
| Show all posts
55555555555555
I want code, urgent use! ! !
A big brother above told me the code in descending order.
Reply

Use magic Report

0

Threads

8

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-13 10:15:01
| Show all posts
It can also be traversed at once, two together
int mint=a[0],maxt = a[0];
for(int i = 0; i <sizeof(a)/sizeof(int); ++i)
{
   if(maxt <a[i])
     //.......;
   if(mint> a[i])
     //........;
}
Reply

Use magic Report

0

Threads

8

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-13 21:00:01
| Show all posts
Exchange the two.
Then at the end, give maxt to the first and mint to the last, and it’s ok,
You have to write a little by yourself?
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-14 10:30:01
| Show all posts
int maxIndex=0;//Maximum position
int minIndex=0;//The minimum position
int maxArray=array[0];
int minArray=array[0];

for(int i=1;i<len;i++)
{
  if(maxArray<array[i])
  {
   maxArray=array[i];
   maxIndex=i;
  }
  if(minArray>array[i])
  {
   minArray=array[i];
   mixIndex=i;
  }
}

int tmpInt=array[0];
array[0]=array[maxIndex];
array[maxIndex]=tmpInt;

tmpInt=array[len-1];
array[len-1]=array[minIndex];
array[maxIndex]=tmpInt;
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-14 11:00:01
| Show all posts
Ha ha, you must be a novice
The two lines above are:
if(maxt<a[i])
  maxt = a[i];
if(mint>a[i])
  mint = a[i];

Finally, assign maxt to a[0] and mint to a[9]
Reply

Use magic Report

0

Threads

8

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-14 20:30:01
| Show all posts
It’s better to exchange, so that the array itself is destroyed
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-15 07:00:01
| Show all posts
Sorry, the above is wrong
Not just assignment, but exchange
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-15 16:00:02
| Show all posts
int maxIndex=0;//Maximum position
int minIndex=0;//The minimum position
int maxArray=array[0];
int minArray=array[0];

for(int i=1;i<len;i++)
{
  if(maxArray<array[i])
  {
   maxArray=array[i];
   maxIndex=i;
  }
  if(minArray>array[i])
  {
   minArray=array[i];
   mixIndex=i;
  }
}

int tmpInt=array[0];
array[0]=array[maxIndex];
array[maxIndex]=tmpInt;

tmpInt=array[len-1];
array[len-1]=array[minIndex];
array[minIndex]=tmpInt;//Typographical error here, changed! ! !

The idea is to write down the position of the maximum and minimum value, and finally exchange

Does this fit your meaning?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list