直接先把m加入到数组中
冒泡或者用sort函数做比较简单
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
int m, n;
int a[101];
while (scanf_s("%d", &n) != EOF)
{
int i, t;
cin >> m;
if (m == 0 && n == 0)
goto pp;
for (i = 0; i < n; i++)
{
cin >> a[i];
}
a[n] = m;
for (i = 0; i <= n; i++)
{
for (int j = 0; j < (n - i); j++)
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
for (int i = 0; i <= n; i++)
{
if (i == n)
cout << a[i] << endl;
else
cout << a[i] << " ";
}
}
pp:
return 0;
}
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
int m, n;
int a[101];
while (scanf_s("%d", &n) != EOF)
{
int i;
cin >> m;
if (m == 0 && n == 0)
goto pp;
for (i = 0; i < n; i++)
{
cin >> a[i];
}
a[n] = m;
sort(a,a+i+1);
for (int i = 0; i <= n; i++)
{
if (i == n)
cout << a[i] << endl;
else
cout << a[i] << " ";
}
}
pp:
return 0;
}