2892. 文件排序

10175102262 LarsPendragon

说好的每组数据之间加一个空行呢(捂脸)example也没加啊(捂脸)不多说了,上C代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {char date[11]; char time[6]; int size; char name[70];}File;
int cmptime(const void*a, const void*b)
{
    File *p1=(File*)a, *p2=(File*)b;
    if(!strcmp(p1->date, p2->date))
    {
        if(!strcmp(p1->time, p2->time))
            return strcmp(p1->name, p2->name);
        return strcmp(p1->time, p2->time);
    }
    return strcmp(p1->date, p2->date);
}
int cmpname(const void*a, const void*b)
{
    File *p1=(File*)a, *p2=(File*)b;
    return strcmp(p1->name, p2->name);
}
int cmpsize(const void*a, const void*b)
{
    File *p1=(File*)a, *p2=(File*)b;
    if(p1->size==p2->size) return strcmp(p1->name, p2->name);
    return p1->size-p2->size;
}
int main()
{
    int n;
    char command[6], list[5];
    scanf("%d",&n);
    while(n)
    {
        File file[100];
        int i;
        for(i=0; i<n; i++)
            scanf("%s %s %d %s",file[i].date, file[i].time, &file[i].size, file[i].name);
        scanf("%s %s",list,command);
        if(!strcmp(command, "/NAME"))
            qsort(file, n, sizeof(file[0]), cmpname);
        else if(!strcmp(command, "/SIZE"))
            qsort(file, n, sizeof(file[0]), cmpsize);
        else
            qsort(file, n, sizeof(file[0]), cmptime);
        for(i=0; i<n; i++)
            printf("%s %s %16d %s\n",file[i].date, file[i].time, file[i].size, file[i].name);
        printf("\n");
        if(!scanf("%d",&n)) break;
    }
    return 0;
}
SNORLAX

请问这个难道不也是最后一组数据后有一个空行吗?

Brown897

这道题里面getline()不能用,cin了两次

#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
struct node{
    string date,time,name;
    int size;
}arr[maxn];

bool cmp1(const node& a,const node& b){
    return a.name<b.name;
}
bool cmp2(const node& a,const node& b){
    return a.size<b.size;
}
bool cmp3(const node& a,const node& b){
    if (a.date == b.date){
        if(a.time == b.time) return a.name<b.name;
        return a.time<b.time;
    }
    return a.date<b.date;
}

int main() {
    int n;
    while (cin>>n,n){
        for (int i = 0; i < n; ++i) {
            cin>>arr[i].date>>arr[i].time>>arr[i].size>>arr[i].name;
        }
        string order;
        cin>>order;
        cin>>order;
        order = order.substr(1,4);
        if (order == "NAME"){
            sort(arr,arr+n,cmp1);
        } else if (order == "SIZE"){
            sort(arr,arr+n,cmp2);
        }else{
            sort(arr,arr+n,cmp3);
        }
        for (int j = 0; j < n; ++j) {
            printf("%s %s %16d %s\n",arr[j].date.c_str(),arr[j].time.c_str(),arr[j].size,arr[j].name.c_str());
        }
        printf("\n");
    }
    return 0;
}
徐摆渡

getline好像在oj上不能用?所以把两个字符串拼起来判断了一下
输出时设置下域宽

#include <bits/stdc++.h>
using namespace std;

int n;
string s1="LIST /NAME", s2="LIST /SIZE", s3="LIST /TIME";
struct data
{
    string Date, Time;
    int Size;
    string Name;
}d[200];

bool cmp1(data x, data y)
{
    return x.Name<y.Name;
}
void nameSort()
{
    sort(d,d+n,cmp1);
}

bool cmp2(data x, data y)
{
    if(x.Size==y.Size) return x.Name<y.Name;
    return x.Size<y.Size;
}
void sizeSort()
{
    sort(d,d+n,cmp2);
}

bool cmp3(data x, data y)
{
    if(x.Date==y.Date&&x.Time==y.Time) return x.Name<y.Name;
    if(x.Date==y.Date) return x.Time<y.Time;
    return x.Date<y.Date;
}
void timeSort()
{
    sort(d,d+n,cmp3);
}
int main()
{
    while(cin>>n&&n!=0){
        for(int i=0;i<n;++i)
            cin>>d[i].Date>>d[i].Time>>d[i].Size>>d[i].Name;
        char ch=getchar();//读掉第一行的回车
        //string now;
        //getline(cin,now);
        string now1, now2;
        cin>>now1>>now2;
        string now=now1+' '+now2;

        if(now==s1) nameSort();
        else if(now==s2) sizeSort();
        else if(now==s3) timeSort();

        for(int i=0;i<n;++i)
            cout<<d[i].Date<<' '<<d[i].Time<<setw(17)<<d[i].Size<<' '<<d[i].Name<<endl;
        puts("");
    }
    return 0;
}
Echose

神烦,发现编译器居然不能通过strcmp之带空格的字符串,aaaaa!

jhygame

include

include

include

include

include

using namespace std;
typedef struct file
{
string time;
string date;
int size;
string name;
}file;
bool cmpname(const file a, const file b)
{
return a.name>n) { if(n==0) return 0; file ss[n]; for(int i=0;i\>ss[i].date>>ss[i].time>>ss[i].size>>ss[i].name;
string list, comm;
cin>>list>>comm;
switch(comm[1])
{
case ‘N’:sort(ss,ss+n,cmpname);break;
case ‘S’:sort(ss,ss+n,cmpsize);break;
case ‘T’:sort(ss,ss+n,cmptime);break;
}
for(int i=0;i<n;i++)
cout<<ss[i].date<<’ ‘<<ss[i].time<<setw(17)<<ss[i].size<<’ ‘<<ss[i].name<<endl;
cout<<endl;
}

}

10175102213

examples里面的Sort03.in和Sort08.in里似乎有特殊的字符,在ubuntu的shell里面测试会出错,不过提交没问题。
另外题目要求每组数据后一个空行…… Examples里面没有,我怎么会注意到啊QAQ

Li Dao

好多同学都问这题,发一下我的代码吧

include

using namespace std;
struct File
{
string Date,Time;
int Size;
string Name;
};
int n,sortmode;
vector V;

int cmp(const File& aa,const File& bb)
{
if(sortmode==0) return aa.Name<bb.Name;
else if(sortmode==1)
{
if(aa.Size==bb.Size) return aa.Name<bb.Name;
else return aa.Size<bb.Size;
}
else if(sortmode==2)
{
if(aa.Date+aa.Time==bb.Date+bb.Time) return aa.Name<bb.Name;
else return aa.Date+aa.Time<bb.Date+bb.Time;
}
}

void print()
{
for(int i=0;i<V.size();i++) printf(“%s %s%17d %s\n”,V[i].Date.c_str(),V[i].Time.c_str(),V[i].Size,V[i].Name.c_str());
cout<>ret;
return ret;
}

int main()
{
while(scanf(“%d”,&n)==1 && n)
{
V.clear();
for(int i=1;i<=n;i++)
{
string a,b,c,d;
cin>>a>>b>>c>>d;
V.push_back((File){a,b,toint(c),d});
}
string Order;
cin>>Order;cin>>Order;
if(Order[1]==’N’) sortmode=0;
else if(Order[1]==’S’) sortmode=1;
else if(Order[1]==’T’) sortmode=2;
sort(V.begin(),V.end(),cmp);
print();
}
return 0;
}

我是后来写的,竟然1A了,感谢前辈的指点
主要是%17s

Fifnmar

这题,有汉字字符就是来恶心人啊。忽略「有汉字」这一点,普通地写就好了。本地有可能调出来的结果跟样例不一样,这是因为评测机与本地的字符集有可能不一样。

C 代码(C++ 在下面):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct {
    char name[64];
    char date[11];
    char time[6];
    unsigned size;
} FileMeta;

#define N 100

FileMeta files[N];

int cmp_name(FileMeta const* a, FileMeta const* b) {
    return strcmp(a->name, b->name);
}

int cmp_size(FileMeta const* a, FileMeta const* b) {
    if (a->size != b->size) {
        return a->size < b->size ? -1 : 1;
    }
    return strcmp(a->name, b->name);
}

int cmp_time(FileMeta const* a, FileMeta const* b) {
    int ret = strcmp(a->date, b->date);
    if (ret != 0) { return ret; }

    ret = strcmp(a->time, b->time);
    if (ret != 0) { return ret; }

    return strcmp(a->name, b->name);
}

int main() {
    for (;;) {
        unsigned n;
        scanf("%u", &n);
        if (n == 0) break;

        for (unsigned i = 0; i != n; ++i) {
            scanf("%s%s%u%s", files[i].date, files[i].time, &files[i].size, files[i].name);
        }

        char cmd[8];
        scanf("%s", cmd);
        scanf("%s", cmd);

        if (strcmp(cmd, "/NAME") == 0) {
            qsort(files, n, sizeof *files, cmp_name);
        } else if (strcmp(cmd, "/SIZE") == 0) {
            qsort(files, n, sizeof *files, cmp_size);
        } else {
            qsort(files, n, sizeof *files, cmp_time);
        }

        for (unsigned i = 0; i != n; ++i) {
            printf("%s %s %16u %s\n", files[i].date, files[i].time, files[i].size, files[i].name);
        }
        putchar('\n');
    }
}
Fifnmar

C++ 代码:

#include <iostream>
#include <tuple>
#include <algorithm>
#include <cstring>

using namespace std;

struct FileMeta {
    char name[64];
    char date[11];
    char time[6];
    unsigned size;
};

constexpr static size_t N = 100;
FileMeta files[N];

int main() {
    unsigned n;
    string cmd;

    for (;;) {
        cin >> n;
        if (n == 0) break;

        for (unsigned i = 0; i != n; ++i) {
            cin >> files[i].date >> files[i].time >> files[i].size >> files[i].name;
        }

        cin >> cmd >> cmd;

        if (cmd == "/NAME") {
            sort(files, files + n, [](auto const &lhs, auto const &rhs) {
                return strcmp(lhs.name, rhs.name) < 0;
            });
        } else if (cmd == "/SIZE") {
            sort(files, files + n, [](auto const &lhs, auto const &rhs) {
                if (lhs.size != rhs.size) {
                    return lhs.size < rhs.size;
                }
                return strcmp(lhs.name, rhs.name) < 0;
            });
        } else {
            sort(files, files + n, [](auto const &lhs, auto const &rhs) {
                auto tmp = strcmp(lhs.date, rhs.date);
                if (tmp != 0) {
                    return tmp < 0;
                }
                tmp = strcmp(lhs.time, rhs.time);
                if (tmp != 0) {
                    return tmp < 0;
                }
                return strcmp(lhs.name, rhs.name) < 0;
            });
        }

        for (unsigned i = 0; i != n; ++i) {
            printf("%s %s %16u %s\n", files[i].date, files[i].time, files[i].size, files[i].name);
        }
        putchar('\n');
    }
}
10152130146

吃空格要用getchar(); !!!!
千万不要用 fflush(stdin);

不知道为什么服务器就是不接受!!??
难道是因为这个函数和读写文件有那么点关系被服务器禁用了??
[em:02][em:02][em:02]
[em:15][em:15][em:15]
[em:17]

你当前正在回复 博客/题目
存在问题!