第二期训练第六题(HDU-2019)

问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2019

问题简述:在n个有序数中插入一个数字,并使新的序列有序。

Point:包含多个测试实例。

AC代码:

#include <iostream>
using namespace std;
int main()
{
    int n,x,a,b[101],i,j;
    while (cin >> n >> x && n&&x)
    {
        b[100] = { 0 };
        for (i = 0; i < n; i++)
        {
            cin >> b[i];
        }
        for (i = 0; i < n; i++)
        {
            if (x<b[i])
            {
                for (j = n - 1; j > i-1; j--)
                {
                    b[j + 1] = b[j];
                }
                b[i] = x;
                break;
            }
        }
        for (i = 0; i < n; i++)
        {
            cout << b[i] << " ";
        }
        cout << b[n] << endl;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43973189/article/details/84996742