820 字
4 分钟
L1-031~L1-035的笔记
Waiting for api.github.com...
P.S. 这份笔记同步发布在我的博客上,建议在博客中查看以享受完整的MD Extended Features. 你可以点此快速跳转到相应页面,我的博客地址为https://samera2022.github.io
L1-031
代码部分
#include <bits/stdc++.h>using namespace std;typedef long long ll;//spec A 9/10
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 H; cin>>H; int W; cin>>W; const double sW = (H-100) * 0.9 * 2; const double delta = abs(W-sW); if (delta<0.1*sW) cout<<"You are wan mei!\n"; else if (sW>=W) cout<<"You are tai shou le!\n"; else if (sW<=W) cout<<"You are tai pang le!\n"; } return 0;}笔记部分
- 采用
if else-if的写法能够更加准确地进行判断。如果在else-if中仍然采用1.1*sW<=W和0.9*sW>=W的写法,很容易因为精度误差而发生错误。
L1-032
代码部分
#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; char delimiter; cin>>N>>delimiter; string content; cin.ignore(); getline(cin,content); if (const int len = static_cast<int>(content.length()); N>=len) { for (int i = 1; i <= N-len; i++) cout<<string(1,delimiter); cout<<content; } else cout<<content.substr(len-N,N); return 0;}笔记部分
- 此处是
getline()的第二种用法,即读取输入的整行内容到给定的字符串中。有的时候输入的数据中含有空格,却仍然要求读取整行时便会使用这种写法。特别地,由于cin读掉内容之后仍然会留下换行符或者空格,因此如果和cin连用,需要再次使用cin.ignore()或ws(cin)忽略这些空白字符。
L1-033
代码部分
#include <bits/stdc++.h>using namespace std;typedef long long ll;
void complete(string& y);int getTimes(const string &y);void add(string& y);
set<char> temp;int times;int len;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); string y; int n; cin>>y>>n; complete(y); while (getTimes(y)!=n) add(y); cout<<times<<" "<<y; return 0;}
void complete(string& y) { len = static_cast<int>(y.length()); for (int i = 1; i <= 4-len; i++) y.insert(0,"0");}
int getTimes(const string& y) { len = static_cast<int>(y.length()); for (int i = 1; i <= len; i++) temp.insert(y[i-1]); const int time = static_cast<int>(temp.size()); temp.clear(); return time;}
void add(string& y) { times++; int time = stoi(y); time++; y = to_string(time); complete(y);}笔记部分
- 这种方法看起来比较臃肿,但是我暂时也没想到更好的方法))
- C++中在
string的末端添加内容一般使用string.append()方法,而在前端添加内容则需要使用string.insert(0,content)方法。特别地,insert()方法本身是用来在某位置插入某内容的,在此处因为目标在开头所以填入索引为0。 - 清空
set中所有元素的方法为set.clear()
L1-034
代码部分
#include <bits/stdc++.h>using namespace std;typedef long long ll;
map<int,int> tagMap;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int times; cin>>times; for (int i = 1; i <= times; i++) { int time; cin>>time; for (int j = 1; j <= time; j++) { int tag; cin>>tag; tagMap[tag]++; } } int keyM = 0; int valM = 0; for (auto const& [key, val]: tagMap) { if (val>valM) { keyM = key; valM = val; } else if (val==valM) { if (key>keyM) { keyM = key; valM = val; } } } cout<<keyM<<" "<<valM; return 0;}笔记部分
- 本段代码中使用的是
结构化绑定遍历,这种遍历要求类型必须为auto。该写法等价于for (const auto& kv : tagMap) { auto key = kv.first, val = kv.second; }
L1-035
代码部分
#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 time = 0; string i2; string i14; for (;;) { string input; cin>>input; if (input==".") break; time++; if (time==2) i2 = input; if (time==14) i14 = input; } if (i2.empty()) cout<<"Momo... No one is for you ..."; else if (i14.empty()) cout<<i2<<" is the only one for you..."; else cout<<i2<<" and "<<i14<<" are inviting you to dinner..."; return 0;}笔记部分
- 没有什么好记的。
L1-031~L1-035的笔记
https://samera2022.github.io/posts/Notes/GPLT/l1-031l1-035/