#B5. 结构体与简单排序题解

结构体与简单排序题解

A1176 谁考了第k名

题意

已知每个学生的学号和成绩,求考第k名学生的学号和成绩。

题解

结构体排序。

%g用来输出实数,它根据数值的大小,自动选f格式或e格式(选择输出时占宽度较小的一种),且不输出无意义的0。即%g是根据结果自动选择科学记数法还是一般的小数记数法 printf("%g\n", 0.00001234); printf("%g\n", 0.0001234);

printf("%.2g\n", 123.45); printf("%.2g\n", 23.45);

上面四句输出结果为: 1.234e-05 0.0001234 1.2e+02 23

对于指数小于-4或者大于给定精度的数值,按照%e的控制输出,否则按照%f的控制输出。

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

struct student {
    int no;
    double num;
};

bool cmp(student b, student c) {
    return b.num > c.num;
}

int main() {
    int n, k;
    student s[105];
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> s[i].no >> s[i].num;
    }
    sort(s, s + n, cmp);
    printf("%d %g", s[k - 1].no, s[k - 1].num);
    return 0;
}

A1178 成绩排序

题意

给出班里某门课程的成绩单,请你按成绩从高到低对成绩单排序输出,如果有相同分数则名字字典序小的在前。

题解

结构体排序,注意分数排序、名字排序。

//C23panweiming
#include<bits/stdc++.h>
using namespace std;
struct st {
    string name;
    int score;
};
bool cmp(st l, st f) {
    if(l.score != f.score)
        return l.score > f.score;
    else
        return l.name < f.name;
}
int main() {
    int n;
    cin >> n;
    st t[n];
    for(int i = 0; i < n; i++) {
        cin >> t[i].name >> t[i].score;
    }
    sort(t, t + n, cmp);
    for(int i = 0; i < n; i++) {
        cout << t[i].name << " " << t[i].score << endl;
    }
    return 0;
}

A1179 奖学金

题意

给定排序规则,输出前5名学生的学号和总分。

题解

结构体排序,注意总分排序、语文排序、学号排序。

//C23fengrui
#include <bits/stdc++.h>
using namespace std;
int n;
struct stu {
    int chinese, maths, english;
    int tot, id;
};
stu a[310];
bool cmp(stu A, stu B) {
    if(A.tot != B.tot)
        return A.tot > B.tot;
    else if(A.chinese != B.chinese)
        return A.chinese > B.chinese;
    else
        return A.id < B.id;
}
int main() {
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> a[i].chinese >> a[i].maths >> a[i].english;
        a[i].tot = a[i].chinese + a[i].maths + a[i].english;
        a[i].id = i;
    }
    sort(a + 1, a + n + 1, cmp);
    for(int i = 1; i <= 5; i++) {
        cout << a[i].id << " " << a[i].tot << endl;
    }
    return 0;
}

A1183 病人排队

题意

给定排序规则,输出排序结果。

题解

结构体排序,注意老年人非老年人排序、年龄排序、登记顺序排序。

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

struct cc {
    int sx;
    string  uid;
    int age;

};

bool px(cc x, cc y) {
    if (x.age >= 60 && y.age >= 60) {
        if (x.age == y.age) {
            return x.sx < y.sx;
        }
        return x.age > y.age;
    }
    if (x.age >= 60) {
        return true;
    } else if(y.age >= 60) {
        return false;
    }
    return x.sx < y.sx;
}

int main() {
    int n;
    cc student[350] = {};
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> student[i].uid >> student[i].age;
        student[i].sx = i + 1;
    }
    sort(student, student + n, px);
    for (int i = 0; i < n; i++) {
        cout << student[i].uid << endl;
    }
    return 0;
}

A1184 明明的随机数

题意

数组去重排序。

题解

方法1:直接排序,统计不重复的数并输出。

方法2:计数排序的思想,使用一个额外的数组存储原数组的出现次数。

方法3:使用STL的set(集合)。

方法4:使用去重函数unique。

//方法1:C23liupeilin
#include<bits/stdc++.h>
using namespace std;
int a[105], ans[105];
int main() {
    int b;
    cin >> b;
    int c = 0;
    for(int i = 0; i < b; i++) {
        cin >> a[i];
    }
    sort(a, a + b);
    for(int i = 0; i < b; i++) {
        if(i == 0 || a[i] != a[i - 1]) {
            ans[c] = a[i];
            c++;
        }
    }
    cout << c << endl;
    for(int i = 0; i < c; i++)
        cout << ans[i] << " ";
    return 0;
}

//方法2:C23fengrui
#include<bits/stdc++.h>
using namespace std;
int n, a[10001], b[1001], cnt;
int main() {
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        if(b[a[i]] == 0) {
            b[a[i]] = 1;
            cnt++;
        }
    }
    cout << cnt << endl;
    for(int i = 1; i <= 1000; i++) {
        if(b[i])cout << i << " ";
    }
    return 0;
}

//方法3:C23panweiming
#include<iostream>
#include<set>
using namespace std;
int main() {
    set<int>s;
    int t;
    cin >> t;
    for(int i = 0; i < t; i++) {
        int x;
        cin >> x;
        s.insert(x);
    }
    cout << s.size() << endl;
    for(set<int>::iterator i = s.begin(); i != s.end(); i++) {
        cout << *i << " ";
    }
    return 0;
}

//方法4
#include<bits/stdc++.h>
using namespace std;
int a[105];
int main() {
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    sort( a, a + n);
    int num = unique(a, a + n) - a;
    cout << num << endl;
    for (int i = 0; i < num; i++)
        cout << a[i] << " ";
    return 0;
}

A1185 单词排序

题意

单词排序去重。

题解

方法1:排序,不输出重复的单词。

方法2:用set。

//方法1:C23lvzekai
#include<bits/stdc++.h>
using namespace std;
string s[110];
bool cmp(string a, string b) {
    return a < b;
}
int main() {
    int t = 0;
    while(cin >> s[t++]);
    sort(s, s + t, cmp);
    for(int i = 0; i < t; i++) {
        if(s[i + 1] != s[i])cout << s[i] << endl;
    }
    return 0;
}

//方法2:C23huangyuran
#include<iostream>
#include<string>
#include<set>
using namespace std;
int main() {
    set<string> s;
    string a;
    while(cin >> a)s.insert(a);
    auto sit = s.begin();
    while(sit != s.end()) {
        cout << *sit << endl;
        sit++;
    }
    return 0;
}

A1447 冒泡排序

题意

输出冒泡排序过程。

题解

//C23pengweixuan
#include<bits/stdc++.h>
using namespace std;
int n;
int a[105];
int main() {
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n - i - 1; j++) {
            if(a[j] > a[j + 1]) {
                swap(a[j], a[j + 1]);
            }
        }
        for(int i = 0; i < n; i++) cout << a[i] << ' ';
        cout << endl;
    }
    return 0;
}

A1448 选择排序

题意

输出选择排序过程。

题解

//C23huangzhiyuan
#include<iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for(int i = 0; i < n; i++) {
        int m = i;
        for(int j = i; j < n; j++) {
            if(a[j] < a[m]) {
                m = j;
            }
        }
        swap(a[i], a[m]);
        for(int j = 0; j < n; j++) {
            cout << a[j] << " ";
        }
        cout << endl;
    }
    return 0;
}

A1449 插入排序

题意

输出插入排序过程。

题解

#include<iostream>
using namespace std;
int a[105];
int n;
void insertion_sort() {
    int i, j, key;
    for (i = 1; i < n; i++) {
        key = a[i];
        j = i - 1;
        while((j >= 0) && (a[j] > key)) {
            a[j + 1] = a[j];
            j--;
        }
        a[j + 1] = key;
        for(j = 0; j < n; j++) 
            cout << a[j] << ' ';
        cout << endl;
    }
    for(j = 0; j < n; j++) 
        cout << a[j] << ' ';
    cout << endl;
}

int main() {
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    insertion_sort();
    return 0;
}