857 字
4 分钟
L1-026~L1-030的笔记
Waiting for api.github.com...
P.S. 这份笔记同步发布在我的博客上,建议在博客中查看以享受完整的MD Extended Features. 你可以点此快速跳转到相应页面,我的博客地址为https://samera2022.github.io
L1-026
代码部分
#include <bits/stdc++.h>using namespace std;typedef long long ll;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout<<"I\n \nL\no\nv\ne\n \nG\nP\nL\nT"; return 0;}笔记部分
- 没有什么好记的。
L1-027
代码部分
#include <bits/stdc++.h>using namespace std;typedef long long ll;
void solve(const string& tel);
int num[10];
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); string tel; cin>>tel; solve(tel); return 0;}
void solve(const string& tel) { vector<int> used; vector<int> index; const int len1 = static_cast<int>(tel.length()); for (int i = 1; i <= len1; i++) num[tel[i-1]-'0']++; for (int i = 10; i >= 1; i--) if (num[i-1]!=0) used.push_back(i-1); cout<<"int[] arr = new int[]{"; const int len2 = static_cast<int>(used.size()); for (int i = 1; i <= len2; i++) cout << used[i-1] << (i == len2 ? "" : ","); cout<<"};\n"; for (int i = 1; i <= len1; i++) if (auto it = find(used.begin(), used.end(), tel[i-1] - '0'); it != used.end()) index.push_back(static_cast<int>(distance(used.begin(),it))); cout<<"int[] index = new int[]{"; const int len3 = static_cast<int>(index.size()); for (int i = 1; i <= len3; i++) cout << index[i-1] << (i == len3 ? "" : ","); cout<<"};\n";}笔记部分
for (int i = 1; i <= len1; i++) if (auto it = find(used.begin(), used.end(), tel[i-1] - '0'); it != used.end()) index.push_back(static_cast<int>(distance(used.begin(),it)));- C++数组中查询某元素的索引写法比较独特,要通过
iterator来实现。if (auto it = find(used.begin(), used.end(), tel[i-1] - '0'); it != used.end())这一句是指从vector的开始到结束,查找并返回tel[i-1]-'0''的位置,且当该位置不为最后时,执行如下操作。 而后通过distance计算当前迭代器所指的位置和最开始的位置的距离。 - 一般地,针对普适的遍历,我们只需要:
for (auto it = begin(s); it != end(s); it++)即可。
L1-028
代码部分
#include <bits/stdc++.h>using namespace std;typedef long long ll;//spec A 4/10
const string& isPrime(ll n);string YES = "Yes";string NO = "No";
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int N; cin>>N; for (int i = 1; i <= N; i++) { int num; cin>>num; cout<<isPrime(num)<<"\n"; } return 0;}
ll llPow(ll a, ll b, const ll mod) { ll res = 1; a %= mod; while (b > 0) { if (b % 2 == 1) res = static_cast<ll>(static_cast<__int128_t>(res) * a % mod);; a = a * a % mod; b /= 2; } return res;}
bool mrTest(const ll n, const ll a) { if (a % n == 0) return true; ll d = n - 1; while (d % 2 == 0) d /= 2; ll x = llPow(a, d, n); if (x == 1 || x == n - 1) return true; while (d != n - 1) { x = static_cast<ll>(static_cast<__int128_t>(x) * x % n); d *= 2; if (x == n - 1) return true; if (x == 1) return false; } return false;}
const string& isPrime(const ll n) { if (n < 2) return NO; if (n == 2 || n == 3) return YES; if (n % 2 == 0) return NO;
static const vector<ll> bases = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
for (const ll a : bases) { if (n == a) return YES; if (!mrTest(n, a)) return NO; } return YES;}笔记部分
- 本题如果用一般的埃氏筛法,可以拿到6分。剩下的4分需要使用更高效的质数算法:Miller-Rabin素数测试。
L1-029
代码部分
#include <bits/stdc++.h>using namespace std;typedef long long ll;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int H; cin>>H; printf("%.1f",(H-100)*0.9*2); return 0;}笔记部分
- 没有什么好记的。
L1-030
代码部分
#include <bits/stdc++.h>using namespace std;typedef long long ll;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int N; cin>>N; string girls[N/2]; string boys[N/2]; string index; int curtG = 1; int curtB = 1; for (int i = 1; i <= N; i++) { int gender; cin>>gender; string name; cin>>name; if (!gender) { girls[curtG-1] = name; curtG++; } else { boys[curtB-1] = name; curtB++; } if (i<=N/2) index.append(to_string(gender)); } curtG = 1; curtB = 1; for (int i = 1; i <= N/2; i++) { if (const int gender = index[i-1]-'0'; !gender) { cout<<girls[curtG-1]<<" "<<boys[N/2-curtG]<<"\n"; curtG++; } else { cout<<boys[curtB-1]<<" "<<girls[N/2-curtB]<<"\n"; curtB++; } } return 0;}笔记部分
- 没有什么好记的。
L1-026~L1-030的笔记
https://samera2022.github.io/posts/Notes/GPLT/l1-026l1-030/