栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

1398 有序数列

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

1398 有序数列

1398 有序数列

时间限制 : 2000/1000 MS(Java/Others) | 内存限制 :65536/32768 KB(Java/Others)

提交数 : 1514 | 通过数 : 568

题目描述
有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。

输入要求
 

输入数据由两行组成,第一行是整数n和x,n表示有n个按升序排列的数据,x表示待插入的数据。

第二行是已经按升序排列的n个数的数列。

输出要求
输出插入新的元素后的数列。

输入样例
3 3
1 2 4

输出样例
1 2 3 4

提示
 

来源
NBU OJ

代码如下:

#include
#include
typedef struct S{
	int id;
	struct S *next;
}stu;
stu* init()
{
	stu*head=(stu*)malloc(sizeof(stu));
	head->id=0;
	head->next=NULL;
	return head;
}
void endinsert(stu*head,int date)
{
	stu*p=head;
	while(p->next!=NULL)
	{
		p=p->next;
	}
	stu*q=(stu*)malloc(sizeof(stu));
	q->id=date;
	q->next=p->next;
	p->next=q;
}
void print(stu*head)
{
	stu*p=head->next;
	int flag=0;
	while(p!=NULL)
	{
		flag++;
		if(flag==1)
		printf("%d",p->id);
		else
		printf(" %d",p->id);
		p=p->next;
	}
	printf("n");
}
void paixu(stu*head)
{
	stu*p=head->next;
	stu*q=head->next;
	for(p;p->next!=NULL;p=p->next)
	{
		for(q=head->next;q->next!=NULL;q=q->next)
		{
			if(q->id>q->next->id)
			{
				int temp=q->id;
				q->id=q->next->id;
				q->next->id=temp;
			}
		}
	}
}
int main()
{
	stu*head=init();
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/686227.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号