首页 > 其他 > 详细

与malloc有关的问题

时间:2016-04-28 00:12:59      阅读:216      评论:0      收藏:0      [点我收藏+]

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

 

与malloc有关的问题

原文:http://www.cnblogs.com/nefu929831238/p/5440817.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!