97. 邮件地址排序

我太难了

C代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
char *getServer(char *s)
{
    while(*s!='@') s++;return s+1;
}
int cmp(const void*a,const void*b)
{
    int t=strcmp(getServer(*(char**)a),getServer(*(char**)b));
    return t?t:strcmp(*(char**)b,*(char**)a);
}
int main()
{
    int i,n;scanf("%d",&n);
    char**s=(char**)malloc(n*sizeof(char*));
    for(i=0;i<n;i++)
    {
        char t[1000001];scanf("%s",t);
        strcpy(s[i]=(char*)malloc(strlen(t)+1),t);
    }
    qsort(s,n,sizeof(s[0]),cmp);
    for(i=0;i<n;i++) printf("%s\n",s[i]);
    return 0;
}
Brown897

贴一个比较简短的吧,,,,感觉写的都好长,,,

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;

struct node{
    string a,b;
}arr[maxn];

bool cmp(const node& a,const node& b){
    if (a.b == b.b) return a.a>b.a;
    return a.b<b.b;
}

int main() {
    int n;
    cin>>n;
    string s;
    for (int i = 0; i < n; ++i) {
        cin>>s;
        arr[i].a=s;
        arr[i].b=s.substr(s.find('@')+1);
    }
    sort(arr,arr+n,cmp);
    for (int j = 0; j < n; ++j) {
        cout<<arr[j].a<<endl;
    }
    return 0;
}
aiden
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(const string &a, const string &b)
{
    string user_a(a, 0, a.find("@"));
    string domain_a(a, a.find("@") + 1);
    string user_b(b, 0, b.find("@"));
    string domain_b(b, b.find("@") + 1);
    if (domain_a == domain_b)
        return a > b;
    else
        return domain_a < domain_b;
}

int main()
{
    vector<string> vec;
    int n;
    cin >> n;
    while (n--)
    {
        string buff;
        cin >> buff;
        vec.push_back(buff);
    }
    sort(vec.begin(), vec.end(), cmp);
    for (const auto &i : vec)
        cout << i << endl;
    return 0;
}
Chains.Z

我是相当好奇这题C语言怎么过得了,如果有用C过的大佬,麻烦请把代码贴一下b( ̄▽ ̄)d

Fifnmar

要不要看看我的代码?

https://acm.ecnu.edu.cn/blog/entry/454/

下载地址(2021-02-25 过期):

https://paste.ubuntu.com/p/KHgsdn5cb5/

Fifnmar

然而并没有释放相关的内存,不过还好

BillChen2000

这个题好奇怪,用qsort会在第十个样例WA,用sort就排出来了

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
typedef struct{
    string username;
    string domain;
} email;
int cmp(const void *pa, const void *pb)
{
    email *pta = (email *)pa;
    email *ptb = (email *)pb;
    if (pta->domain != ptb->domain){
        int a = (pta->domain < ptb->domain);
        return pta->domain > ptb->domain ? 1 : -1;
    }
    else
        return pta->username < ptb->username ? 1 : -1;
}
bool cmp_sort(email a, email b){
    if (a.domain != b.domain){
        return a.domain < b.domain;
    }
    else
        return a.username > b.username;
}
int main()
{
    int len;
    scanf("%d", &len);
    email *data;
    data = new email[len];
    while (cin.get() != '\n')
        continue;
    for (int i = 0; i < len; i++){
        char ch;
        while((ch=cin.get())!='@'){
            data[i].username += ch;
        }
        while ((ch = cin.get()) != '\n' && ch != EOF){
            data[i].domain += ch;
        }
    }
    sort(data, data + len, cmp_sort);
    //qsort(data, len, sizeof(data[0]), cmp);
    for (int k = 0; k < len; k++)
        cout << data[k].username << "@" << data[k].domain << endl;
    return 0;
}
Money4
#include<bits/stdc++.h>
using namespace std;
struct Node{
    string user;
    string home;
};
int main(){
    int T;
    cin>>T;
    vector<Node> mails;
    for(int t = 0; t < T; t++){
        string str;
        cin>>str;
        int i = 0;
        while(str[i] != '@'){
            i++;
        }
        Node node;
        node.user = str.substr(0,i);
        node.home = str.substr(i+1,str.length()-i);
        mails.emplace_back(node);
    }
    sort(mails.begin(),mails.end(),[](Node &a,Node &b){
        if(a.home == b.home) return a.user> b.user;
        else return a.home < b.home;
    });
    for(int t = 0 ; t < T ; t++){
        printf("%s@%s\n",mails[t].user.c_str(),mails[t].home.c_str());
    }
}
liuzb13

codeblock的空间开不到50万了吧。。。。

LzQuarter

样例最坏情况有50万个

Canis

这个题java会超时,不知道是不是因为java太慢了还是因为我菜

import java.util.;
import java.text.
;
import java.io.;
import java.time.
;
import java.time.format.*;

class Email implements Comparable
{
private String id, domain;
public Email() {}
public Email(String i, String d) {id = i; domain = d;}
public Email(String str)
{
this(str.split(“@”)[0],str.split(“@”)[1]);
}
public int compareTo(Email other)
{
if(domain.equals(other.domain))//java字符串比较不能用==
{
return -id.compareTo(other.id);
}
else
{
return domain.compareTo(other.domain);
}
}
public void print()
{
System.out.println(id + “@” + domain);
}
}

public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
Email[] a = new Email[num];
String str;
for(int i = 0; i < num; ++i)
{
str = sc.next();
Email temp = new Email(str);
a[i] = temp;
}
Arrays.sort(a);
for(Email obj : a)
{
obj.print();
}
sc.close();
}
}

coolMarlon

有一个疑问,21行,如果填>= 的话,在第12个用例处会报runtime_error ,大佬帮忙看看?

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
//#include <bits/stdc++.h>
using namespace std;

struct Email{
    string uname;
    string add;
    Email(){}
    Email(string s, string a): uname(s), add(a){}
};
const int MAXN = 1000005;
Email arr[MAXN];
//vector<Email> arr;

bool Compare(Email e1, Email e2){
    if(e1.add == e2.add){
        return e1.uname >= e2.uname;
    }
    return e1.add < e2.add;

}

int main()
{
    int n;
    scanf("%d", &n);
    string str;
    for(int i = 0; i < n; i++){
        cin >> str;
        int pos = str.find("@");
        Email e(str.substr(0,pos), str.substr(pos+1));
        arr[i] = e;
        //arr.push_back(e);
    }
    //sort(arr.begin(), arr.end(), Compare);
    sort(arr, arr+n , Compare);
     for(int i = 0; i < n; i++){
        cout << arr[i].uname << "@" << arr[i].add << endl;
    }
    return 0;
}
会奔跑的bug

all_e = []
n = int(input())
for i in range(n):
one_e = input().split(‘@’)
all_e.append([one_e[0],one_e[1]])
all_e.sort(key=lambda x:x[0],reverse=True)
all_e.sort(key=lambda x:x[1])
for one_ee in all_e:
print(one_ee[0]+’@’+one_ee[1])

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