点点滴滴”

导航

HDU 2019 数列有序!

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!=0||n!=0))
        {
            node * root=(node *)malloc(sizeof(node));    //定义一个头指针 root
            root->next=NULL;
            node *p=root;                  //定义用于连接的指针p
            for(i=1;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 0;
}

/*~~~~~~~~~~~~~~~~~~~还不太会链表  不知道自己哪里错了  过几天看~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#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=0; 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 0;
}

 

posted on 2014-08-28 18:57  点点滴滴”  阅读(176)  评论(0编辑  收藏  举报