#include"stdafx.h"
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
const int t = 8;
int num[t]{ 3,6,4,8,7,2,5,9};//此时可正常排列,若把前面的3改成1排列时如:int num[t]{ 1,6,4,8,7,2,5,9};最后一对排列不正确
//把元素1和2对调生成程序则又正确排列
int i = 0;
int n = 0;
int a = 0;
for (int k = 0; k < t; k++)
{
a = num[k];
for (i=k+1; i < t; i++)//循环找出最大值
{
if (a < num[i])
{
a = num[i];
n = i;
}
}
if (i == t)
{
cout << num[k]<<" "<<k << " " << num[n]<<" "<<n<<endl;
if (num[k] < num[n])
swap(num[k], num[n]);
i = 0;
}
}
for (auto x : num)cout << x << " ";
return 0;
}
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
const int t = 8;
int num[t]{ 3,6,4,8,7,2,5,9};//此时可正常排列,若把前面的3改成1排列时如:int num[t]{ 1,6,4,8,7,2,5,9};最后一对排列不正确
//把元素1和2对调生成程序则又正确排列
int i = 0;
int n = 0;
int a = 0;
for (int k = 0; k < t; k++)
{
a = num[k];
for (i=k+1; i < t; i++)//循环找出最大值
{
if (a < num[i])
{
a = num[i];
n = i;
}
}
if (i == t)
{
cout << num[k]<<" "<<k << " " << num[n]<<" "<<n<<endl;
if (num[k] < num[n])
swap(num[k], num[n]);
i = 0;
}
}
for (auto x : num)cout << x << " ";
return 0;
}