【PAT甲级 补全前导0 vector作为函数参数】1025 PAT Ranking (25 分) Java、C++
生活随笔
收集整理的這篇文章主要介紹了
【PAT甲级 补全前导0 vector作为函数参数】1025 PAT Ranking (25 分) Java、C++
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如何補全前導0
- Java
- C++
容器vector在函數中的參數傳遞
(1)值傳遞: 形參改變不影響實參
void fun(vector<int> v) { }int main() {vector<int>v;fun(v) }(2)引用傳遞 :形參改變影響實參
void fun(vector<int> &v){ }int main(){vector<int> v;fun(v); }(3)指針傳遞:形參改變影響實參
void fun(vector<int>* v){ }int main(){vector<int> v;fun(&v); }題目
題目要求我們把各個考場的考生成績匯總起來,按照要求排名(場內排名+整場排名),最后輸出
要注意的坑
- 最后一個測試點的坑,如果ID用的是long long,注意補前導0
- Java仍然最后一個測試點超時,C++ 沒事
自測用例
3 5 1234567890001 95 1234567890005 100 1234567890003 95 1234567890002 77 1234567890004 85 10 1234567890024 85 1234567890025 89 1234567890026 89 1234567890027 85 1234567890013 65 1234567890011 25 1234567890014 100 1234567890021 85 1234567890022 85 1234567890023 85 10 1234567890124 85 1234567890125 89 1234567890126 89 1234567890127 85 1234567890113 65 1234567890111 25 1234567890114 100 0004567890121 85 0004567890122 85 1234567890023 85題解1:C++(全部AC)
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<vector> #include<algorithm> using namespace std;class Stu { public:long long id;int score;int fRank;// final_rankint locate;// location_rankint lRank;// local_rankStu(long long id, int score, int lo) {this->id = id;this->score = score;this->locate = lo;} };//score排序 int cmpScore(Stu a, Stu b) {return(a.score > b.score); } //id排序 int cmpId(Stu a, Stu b) {return(a.id < b.id); }//組內序號 void localRank(vector<Stu> &list) {list[0].lRank = 1;for (int i = 1; i < list.size(); i++) {if (list[i].score == list[i - 1].score) {list[i].lRank = list[i - 1].lRank;}else {list[i].lRank = i + 1;}} }//整體序號 void finalRank(vector<Stu> &lists) {lists[0].fRank = 1;for (int i = 1; i < lists.size(); i++) {if (lists[i].score == lists[i - 1].score) {lists[i].fRank = lists[i - 1].fRank;}else {lists[i].fRank = i + 1;}} }int main() {int n;cin >> n;int total = 0;vector<Stu> lists;// 讀整體for (int i = 0; i < n; i++) {vector<Stu> list;int k;cin >> k;total += k;for (int j = 0; j < k; j++) {// 讀組內long long id;int score;cin >> id >> score;Stu t(id, score, i + 1);list.push_back(t);}//組內排名stable_sort(list.begin(), list.end(), cmpId);stable_sort(list.begin(), list.end(), cmpScore);localRank(list);for (Stu stu : list) {lists.push_back(stu);}}//整體排名stable_sort(lists.begin(), lists.end(), cmpId);stable_sort(lists.begin(), lists.end(), cmpScore);finalRank(lists);cout << total << "\n";for (Stu out : lists) {printf("%013lld %d %d %d\n", out.id, out.fRank, out.locate, out.lRank);}system("pause"); }題解2:Java
與題解1算法完全相同,最后一個測試點超時
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List;public class Main {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int n = Integer.parseInt(br.readLine());int total = 0;List<Stu> list = new ArrayList<>();// 讀整體for (int i = 0; i < n; i++) {List<Stu> l = new ArrayList<>();int k = Integer.parseInt(br.readLine());total += k;for (int p = 0; p < k; p++) {// 讀組內l.add(new Stu(br.readLine(), i + 1));}Collections.sort(l);// 組內排序localRank(l);// 組內序號list.addAll(l);}Collections.sort(list);// 整體排序finalRank(list);// 整體序號System.out.println(total);for (Stu out : list) {String idStr = String.format("%013d", out.id);System.out.println(idStr + " " + out.fRank + " " + out.locate + " " + out.lRank);}}public static void localRank(List<Stu> l) {l.get(0).lRank = 1;for (int j = 1; j < l.size(); j++) {if (l.get(j).score == l.get(j - 1).score) {l.get(j).lRank = l.get(j - 1).lRank;} else {l.get(j).lRank = j + 1;}}}public static void finalRank(List<Stu> list) {list.get(0).fRank = 1;for (int j = 1; j < list.size(); j++) {if (list.get(j).score == list.get(j - 1).score) {list.get(j).fRank = list.get(j - 1).fRank;} else {list.get(j).fRank = j + 1;}}} }class Stu implements Comparable<Stu> {long id;int score;int fRank;// final_rankint locate;// location_rankint lRank;// local_rankpublic Stu(String s, int lo) {String[] arr = s.split(" ");this.id = Long.parseLong(arr[0]);this.score = Integer.parseInt(arr[1]);this.locate = lo;}@Overridepublic int compareTo(Stu o) {if (score > o.score) {return -1;} else if (score < o.score) {return 1;} else {if (id > o.id) {return 1;} else if (id < o.id) {return -1;} else {return 0;}}} }附:一個SE同學的題解(C++)全部AC
#define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <algorithm> #include <vector>using namespace std;struct student {long long int no;int score, finrank, loca, locarank; };bool cmp1(student a, student b) {return a.score != b.score ? a.score > b.score:a.no < b.no; } int main() {int n, m;scanf("%d", &n);vector<student> fin; //最后的學生排序數組 for (int i = 1; i <= n; i++){scanf("%d", &m);vector<student> v(m); //當前考場學生數組 for (int j = 0; j < m; j++){scanf("%lld %d", &v[j].no, &v[j].score);v[j].loca = i;}sort(v.begin(), v.end(), cmp1);v[0].locarank = 1;fin.push_back(v[0]);for (int j = 1; j < m; j++){v[j].locarank = (v[j].score == v[j - 1].score) ? (v[j - 1].locarank) : (j + 1);fin.push_back(v[j]);}}sort(fin.begin(), fin.end(), cmp1);fin[0].finrank = 1;for (int j = 1; j < fin.size(); j++){fin[j].finrank = (fin[j].score == fin[j - 1].score) ? fin[j - 1].finrank : j + 1;}printf("%d\n", fin.size());for (int i = 0; i < fin.size(); i++){printf("%013lld %d %d %d\n", fin[i].no, fin[i].finrank, fin[i].loca, fin[i].locarank);}// printf("%d",v[0].score);system("pause"); }總結
以上是生活随笔為你收集整理的【PAT甲级 补全前导0 vector作为函数参数】1025 PAT Ranking (25 分) Java、C++的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【PAT甲级 环最短距离】1046 Sh
- 下一篇: 【Java设计模式】GOF32 - 单例