while 循环语句应用
随机输入三个整数,如何使用while语句将三个数从大到小按顺序排列?
急用。
[解决办法]
#include <iostream>
using namespace std;
int main()
{
int *a = new int[3];
cout << "Please input three int :\n";
int i= 0;
int temp = 0;
while(i< 3 && cin >> temp)
{
if(i == 0)
{
a[i] = temp;
}
if( i > 0)
{
int j = i;
while( j >= 1 && temp > a[j-1])
{
a[j] = a[j-1];
--j;
}
a[j] = temp;
}
++i;
}
cout << a[0]<<'\t'<< a[1]<<'\t'<<a[2]<<endl;
return 0;
}