Time Limit: 1000 MS Memory Limit: 32768 KB

64-bit integer IO format: %I64d , %I64u   Java class name: Main

[Submit] [Status] [Discuss]

Description

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

Input

输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。

Output

对于每个测试实例,输出插入新的元素后的数列。

Sample Input

3 3
1 2 4
0 0

Sample Output

1 2 3 4
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
node *next;
};
int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)!=EOF&&(m!=||n!=))
{
node * root=(node *)malloc(sizeof(node)); //定义一个头指针 root
root->next=NULL;
node *p=root; //定义用于连接的指针p
for(i=;i<=n;i++) //依次开辟新空间,存入数据,并且一节一节的连接
{
scanf("%d",&p->num);
node *temp=(node *)malloc(sizeof(node));
temp->next=NULL;
p->next=temp;
p=temp;
}
p=root;
while(p->next!=NULL) //这里开始插入数字。
{
if(p->next->num>=m)
{
node *temp=(node *)malloc(sizeof(node)); //为新数字开辟内存。并连接。
temp->num=m;
temp->next=p->next; p->next=temp;
break;
}
p=p->next;
} p=root;
while(p->next!=NULL) //输出链表的数据。最后一节的next指针为空。用来结束。
{
if(p==root)
printf("%d",p->num);
else
printf(" %d",p->num);
p=p->next;
}
printf("\n"); }
return ;
} /*~~~~~~~~~~~~~~~~~~~还不太会链表  不知道自己哪里错了  过几天看~~~~~~~~~~~~~~~~~~~~~~~~~~*/ #include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> using namespace std; struct node
{
node *next;
int num;
}; int main()
{
int n,m;
while(scanf("%d%d",&n,&m),n,m)
{
node *root=(node *)malloc(sizeof(node)); ///定义一个头指针 名为root 给node分配一块空间
node *p=root; ///表头指针 定义一个链接的指针p 指向分配的空间的开始部位
root->next=NULL; ///初始化吧~~~~~~~~~~~~
for(int i=; i<n; i++)
{
scanf("%d",&p->num);
node *temp=(node *)malloc(sizeof(node)); ///给temp分配一块空间
temp->next=NULL;
p->next=temp;
p=temp;
}
p=root;
while(p->next!=NULL)
{
if(p->next->num>m)
{
node *temp=(node *)malloc(sizeof(node)); ///为新数字开辟内存并链接
temp->num=m;
temp->next=p->next;
break;
}
p=p->next;
}
p=root;
while(p->next!=NULL) ///输出链表的数据。最后一节的next指针为空。用来结束。
{
if(p==root)
printf("%d",p->num);
else
printf(" %d",p->num);
p=p->next;
}
printf("\n");
}
return ;
}

HDU 2019 数列有序!的更多相关文章

  1. hdu 2019:数列有序!(数据结构,直接插入排序+折半插入排序)

    数列有序! Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

  2. HDOJ 2019 数列有序!

    #include<vector> #include<iostream> #include<algorithm> #include<cstdio> usi ...

  3. 杭电2019 数列有序!(STL解法)

    由于这题对于学过数据结构的我来说,真的是很简单,为了减少时间上的损失,链表无疑是最好的选择(因为数组要往后移位子).然后,因为最近想玩些STL的骚操作,所以就用<list>了,然后顺便学了 ...

  4. HDOJ2019数列有序!

    数列有序! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. 新疆大学OJ(ACM) 1099: 数列有序!

    1099: 数列有序! 时间限制: 1 Sec  内存限制: 128 MB 题目描述 有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的 ...

  6. hdu 2019

    Problem Description 有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序.   Input 输入数据包含多 ...

  7. HDU2019数列有序!

    Problem Description 有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序. Input 输入数据包含多个测 ...

  8. hdu 5090 数列贪心加成1~n

    http://acm.hdu.edu.cn/showproblem.php?pid=5090 给一段长度为n数列,问能否给任意个数加上k的倍数,使得加完之后恰好只有1~n 贪心,先排序,依次加出1~n ...

  9. hdu 5086 数列连续和求和

    http://acm.hdu.edu.cn/showproblem.php?pid=5086 求一段数列里面所有连续和的和,卡精度 规律很明显,数列里面每个数都被加了i*(n+1-i)次 注意下精度即 ...

随机推荐

  1. PCI Express(一)- Connector

    在FPGA4FUN上看到一篇介绍PCI-E的帖子,简单易懂,适合入门,特地搬过来 原文地址:http://www.fpga4fun.com/PCI-Express.html 前言: As PCI Ex ...

  2. swift 存储属性和计算属性 set{}和get{} didSet{}和willSet{}

    首先我们看一段代码: var A:Int = 0 var B:Int = 0 var C:Int { get { return 1 } set { print("Recived new va ...

  3. submit异步提交 回调的方法

    背景: mvc模式下,当submit表单的时候,后台Control自动绑定Model类,但是如果不用submit来提交,改用ajax提交的时候,后台Control无法获取前台form表单内相应Name ...

  4. nginx的一些介绍和使用

    nginx 的安装 我们首先进行下载安装:http://nginx.org/download/nginx-1.4.2.tar.gz 安装准备: nginx依赖于pcre库,要先安装pcre 1 yum ...

  5. “VS2013无法连接远程数据库”解决方案

    “VS2013无法连接远程数据库” 解决方案:以管理员身份登录CMD,输入netsh winsock reset并回车(注意,必须是已管理员身份运行,这个重置LSP连接) 或 netsh winsoc ...

  6. jQuery Mobile 脚本加载问题

    刚开始使用jQuery Mobile,发现很多问题需要重新考虑,比如脚本加载问题. 在普通html中,如果a.html中有链接到b.html,b.html中有类似代码: $(document).rea ...

  7. ---解决git pull 后出现冲突的解决方法

    0. git statusOn branch masterYour branch and 'origin/master' have diverged,and have 1 and 3 differen ...

  8. [Tomcat 源码分析系列] (附件) : setclasspath.bat 脚本

    @echo off rem Licensed to the Apache Software Foundation (ASF) under one or more rem contributor lic ...

  9. JStorm注意事项

    storm.yaml的配置可以参照defaults.yaml,例如: nimbus.childopts: " -Xms256m -Xmx256m -XX:+UseConcMarkSweepG ...

  10. VC++ CEdit

    CEDIT _1, //selection pEdit1->SetSel(0,strBuffer - m_strInput,0); pEdit1->SetFocus(); //the se ...