与malloc有关的问题

nefu 1026 

申请动态空间存放字符串,将其排序后输出

http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1026

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

struct node
{
    char str[55];
} data[25];

bool cmp(const node &a,const node &b)
{
    if(strcmp(a.str,b.str)<0) return true;
    else return false;
}

int main()
{
    int n;
    node *p,tmp;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            p=(struct node *)malloc(sizeof(struct node));
            cin>>p->str;
            strcpy(data[i].str,p->str);
        }
        sort(data,data+n,cmp);
        for(int i=0;i<n;i++)
        cout<<data[i].str<<endl;
    }
    return 0;
}
View Code

 

posted @ 2016-04-27 22:56  邻家那小孩儿  阅读(80)  评论(0编辑  收藏  举报