'commit'
This commit is contained in:
23
GESP/Level5/Eratosthenes/1.cpp
Normal file
23
GESP/Level5/Eratosthenes/1.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
const int N = 210;
|
||||
int b[N];
|
||||
|
||||
int n = 200; // 200以内的质数
|
||||
int main() {
|
||||
// 初始化标记数组中所有数字都是质数
|
||||
memset(b, 1, sizeof b);
|
||||
|
||||
for (int i = 2; i <= n; i++) {
|
||||
if (b[i])
|
||||
for (int j = i * 2; j <= n; j += i) // 倍数都是合数
|
||||
b[j] = 0;
|
||||
}
|
||||
|
||||
// 输出有哪些质数
|
||||
for (int 2 = 1; i <= n; i++)
|
||||
if (b[i])
|
||||
cout << i << " ";
|
||||
|
||||
return 0;
|
||||
}
|
||||
24
GESP/Level5/Eratosthenes/2.cpp
Normal file
24
GESP/Level5/Eratosthenes/2.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
const int N = 210;
|
||||
int b[N];
|
||||
|
||||
int n = 200; // 200以内的质数
|
||||
int main() {
|
||||
// 初始化标记数组中所有数字都是质数
|
||||
memset(b, 1, sizeof b);
|
||||
|
||||
// 理论依据:任何非质数,都会有一个因数不会大于他的算术平方根。所以优化后代码:
|
||||
for (int i = 2; i <= sqrt(n); i++) { // 优化上限n到sqrt(n)
|
||||
if (b[i])
|
||||
for (int j = i * 2; j <= n; j += i) // 倍数都是合数
|
||||
b[j] = 0;
|
||||
}
|
||||
|
||||
// 输出有哪些质数
|
||||
for (int i = 2; i <= n; i++)
|
||||
if (b[i])
|
||||
cout << i << " ";
|
||||
|
||||
return 0;
|
||||
}
|
||||
25
GESP/Level5/Eratosthenes/3.cpp
Normal file
25
GESP/Level5/Eratosthenes/3.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
const int N = 210;
|
||||
int b[N];
|
||||
|
||||
int n = 200; // 200以内的质数
|
||||
int main() {
|
||||
// 初始化标记数组中所有数字都是质数
|
||||
memset(b, 1, sizeof b);
|
||||
|
||||
// 理论依据:任何非质数,都会有一个因数不会大于他的算术平方根。所以优化后代码:
|
||||
for (int i = 2; i <= sqrt(n); i++) { // 优化上限n到sqrt(n)
|
||||
if (b[i])
|
||||
// 根据观察,每次筛选的时候从这个数的平方开始筛选就可以。
|
||||
for (int j = i * i; j <= n; j += i) // 倍数都是合数
|
||||
b[j] = 0;
|
||||
}
|
||||
|
||||
// 输出有哪些质数
|
||||
for (int i = 2; i <= n; i++)
|
||||
if (b[i])
|
||||
cout << i << " ";
|
||||
|
||||
return 0;
|
||||
}
|
||||
42
GESP/Level5/Eratosthenes/new
Normal file
42
GESP/Level5/Eratosthenes/new
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
const int N = 10010;
|
||||
struct Node {
|
||||
int id; // 第几个学生
|
||||
int c, m, e; // 语文 数学 英语
|
||||
friend bool operator<(Node a, Node b) {
|
||||
int s1 = a.c + a.m + a.e;
|
||||
int s2 = b.c + b.m + b.e; // 总分
|
||||
if (s1 != s2) // 总分不同 总分大的在前
|
||||
return s1 > s2;
|
||||
if (a.c + a.m != b.c + b.m) // 语文和数学总分不同 语文和数学总分大的在前
|
||||
return a.c + a.m > b.c + b.m;
|
||||
|
||||
if (max(a.c, a.m) != max(b.c, b.m)) // 语文和数学中较大的一个不同 较大的在前
|
||||
return max(a.c, a.m) > max(b.c, b.m);
|
||||
return a.id < b.id; // 语文和数学中较大的一个相同 录入顺序小的在前
|
||||
}
|
||||
} a[N];
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cin >> a[i].c >> a[i].m >> a[i].e; // 同学的语文、数学、外语成绩
|
||||
a[i].id = i; // 同学的录入顺序
|
||||
}
|
||||
sort(a + 1, a + n + 1);
|
||||
|
||||
// 5 3 2 4 1
|
||||
// 100 100 98 98 97
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
// 到a数组中去查找
|
||||
for (int j = 1; j <= n; j++) {
|
||||
if (a[j].id == i) {
|
||||
cout << j << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
1
GESP/Level5/Eratosthenes/参考资料.txt
Normal file
1
GESP/Level5/Eratosthenes/参考资料.txt
Normal file
@@ -0,0 +1 @@
|
||||
https://blog.csdn.net/weixin_40204595/article/details/105405170
|
||||
Reference in New Issue
Block a user