https://www.noobdream.com/DreamJudge/Issue/page/1151/
#include
#include
#include
using namespace std;
const int N = 1010;
struct Stu {
string name;
int score;
bool operator < (const Stu &w) const {
return score < w.score;
}
bool operator > (const Stu &w) const {
return score > w.score;
}
} stu[N];
int main() {
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i ++) {
cin >> stu[i].name >> stu[i].score;
}
if(m == 1)
stable_sort(stu, stu + n);
else
stable_sort(stu, stu + n, greater<Stu>());
for(int i = 0; i < n; i ++)
cout << stu[i].name << " " << stu[i].score << endl;
return 0;
}
https://www.noobdream.com/DreamJudge/Issue/page/1159/
#include
#include
using namespace std;
const int N = 110;
struct Stu {
int id;
int score;
bool operator < (const Stu &w) const {
if(score != w.score)
return score < w.score;
else
return id < w.id;
}
} stu[N];
int main() {
int n;
cin >> n;
for(int i = 0; i < n; i ++) {
cin >> stu[i].id >> stu[i].score;
}
sort(stu, stu + n);
for(int i = 0; i < n; i ++) {
cout << stu[i].id << " " << stu[i].score << endl;
}
return 0;
}
https://blog.csdn.net/horizon_7887/article/details/128911379
#include
#include
using namespace std;
const int N = 1010;
int n;
struct node {
int a;
int sum; //sum是各位和
//从大到小排序,重载大于号
bool operator > (const node &w) const {
if(sum == w.sum)
return a < w.a;
return sum > w.sum;
}
} p[N];
//计算各位和
int add_sum(int a) {
int sum = 0;
while(a) {
sum += a % 10;
a /= 10;
}
return sum;
}
int main() {
cin >> n;
for(int i = 0; i < n; i ++) {
cin >> p[i].a;
p[i].sum = add_sum(p[i].a);
}
sort(p, p + n, greater<node>()); //从大到小排序加参数 greater()
for(int i = 0; i < n; i ++) {
cout << p[i].a << " " << p[i].sum << endl;
}
return 0;
}
https://www.noobdream.com/DreamJudge/Issue/page/1178/
#include
#include
#include
#include
using namespace std;
vector<int> div(vector<int> &A, int b, int r) {
vector<int> C;
for(int i = A.size() - 1; i >= 0; i --) {
r = r * 10 + A[i];
C.push_back(r / b);
r = r % b;
}
reverse(C.begin(), C.end());
while(C.size() > 1 && C.back() == 0)
C.pop_back();
return C;
}
int main() {
string c;
while(cin >> c) {
vector<int> A;
for(int i = c.size() - 1; i >= 0; i --) {
A.push_back(c[i] - '0');
}
string res;
while(true) {
res = res + to_string(A[0] % 2);
A = div(A, 2, 0);
if(A.size() == 1 && A[0] == 0)
break;
}
for(int i = res.size() - 1; i >= 0; i --)
cout << res[i];
cout << endl;
}
return 0;
}
https://www.noobdream.com/DreamJudge/Issue/page/1410/
#include
using namespace std;
int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int is_leap(int year) {
if(year % 100 != 0 && year % 4 == 0 || year % 400 == 0)
return 1;
return 0;
}
int main() {
int y, s;
while(cin >> y >> s) {
int d = 0, m = 1;
if(is_leap(y))
months[2] = 29;
else
months[2] = 28;
while(s --) {
d ++;
if(d > months[m]) {
d = 1;
m ++;
if(m > 12) {
m = 1;
y ++;
}
}
}
printf("%04d-%02d-%02d\n", y, m, d);
}
return 0;
}
https://www.noobdream.com/DreamJudge/Issue/page/1446/
枚举a会超时,枚举m不会
#include
#include
#include
#include
using namespace std;
const int N = 1010;
int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int is_leap(int year) {
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return 1;
return 0;
}
int main() {
int T, y, m, d, a;
cin >> T;
while(T --) {
cin >> y >> m >> d >> a;
is_leap(y) == 1 ? month[2] = 29 : month[2] = 28;
d += a;
//枚举月份
while(d > month[m]) {
d -= month[m];
m ++;
if(m > 12) {
y ++;
m = 1;
is_leap(y) == 1 ? month[2] = 29 : month[2] = 28;
}
}
printf("%4d-%02d-%02d\n", y, m, d);
}
return 0;
}