您的位置:首页 > 其它

hdu 2019 数列有序!

2014-07-14 17:00 351 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2019

题目大意:插入排序,只要要插入的数比左边的打,比右边的小即可插入,输出有序序列!

以下提供两种方法,供参考~

第一种:

#include <stdio.h>
int main ()
{
int i,n,m,a[110];
while (scanf("%d%d",&n,&m)!=EOF)
{
if (n==0&&m==0)
break;
for (i=0; i<n; i++)
scanf("%d",&a[i]);
for (i=0; i<n; i++)
if (m>a[i])
printf ("%d ",a[i]);
else
break;
printf ("%d",m);
for (; i<n; i++)
printf(" %d",a[i]);
printf ("\n");
}
return 0;
}


第二种:

#include <iostream>
#include <cstdio>
using namespace std;
int main ()
{
int i,n,m,a[101];
while (cin>>n>>m)
{
if (n==0&&m==0)
break;
for (i=0; i<n; i++)
cin>>a[i];
for (i=n;a[i-1]>m; i--)
a[i]=a[i-1];
a[i]=m;
for (i=0; i<=n-1; i++)
printf ("%d ",a[i]);
printf ("%d\n",a[i]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: