Entries from 2022-07-01 to 1 month

数値誤差

atcoder.jp を解いていての学び. 参考 drken1215.hatenablog.com 数値誤差 #include <bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ios::sync_with_stdio(false); cin.tie(0); double a= 2.51; cout << (int) (a*100) << endl; return 0; } 250</bits/stdc++.h>…

範囲for文

C++

範囲for文というループの書き方を知らなかったのでメモ. 参考 atcoder.jp 上記サイトからの引用 #include <bits/stdc++.h> using namespace std; int main() { vector<int> a = {1, 3, 2, 5}; for (int x : a) { cout << x << endl; } } 1 3 2 5 範囲for文はコンテナと呼ばれる</int></bits/stdc++.h>…

std::map で要素のカウント

atcoder.jp を解いてて他の方のコードを見ていて知ったのでメモ. この問題は最初に各文字で始まる文字列を数えておくと良いが,自分の実装では #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int</bits/stdc++.h>…

std::set の要素参照

C++

std::set は二分探索木での実装なので operator[] でアクセスできない.イテレータを使う. #include <bits/stdc++.h> using namespace std; int main(){ set<int> st{3, 1, 4}; for (auto itr = st.begin(); itr != st.end(); ++itr) { cout << *itr << endl; } return 0; } 1 3</int></bits/stdc++.h>…

std::set

atcoder.jp 簡単な問題だけど std::set を初めて使ったのでメモ. #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); set<int> s; for (int i = 0; i < 3; i++) { int x; cin >> x; s.insert(x); } if(s.size() == 2) { cout </int></bits/stdc++.h>…

std::cout の小数点以下の表示桁数を変更

C++

cout << setprecision(12) << ans << endl; 以下のように書くと小数点が0でも指定した表示桁数で表示する cout << fixed << setprecision(12) << ans << endl;

1文字だけ大文字から小文字に変換する

atcoder.jp C++ の練習がてら上の問題を解いていて,ググって出てくるのが std::transform とかを使う方法がほとんどだったけど,アルファベットの文字コードが大文字と小文字で 32 異なるということを利用するのがシンプル(だしこの問題の模範解答)だった…