C++競プロ学習日記(仮)

( 学習記録であり解説Blogではないです )

コード

二分法( 二分探索 )で平方根を求める

二分探索の練習をしました。 追記:二分探索と書いたけれど、正しくは二分法 *1 かも知れない...。 追記②:正しくは二分法だった... 整数 および 実数 が与えられるので となるような を絶対 or 相対誤差が となる精度で求めてください。 #include <bits/stdc++.h> using na</bits/stdc++.h>…

ABC072 - C Together|AtCoder

C: Together - AtCoder Beginner Contest 072 | AtCoder を解きました。 題意: 長さ の整数列 が与えられます。 に対し、1 足すか、1 引くか、何もしない、の 3 つの操作が選べるので、ある整数 を選んだ時、 となる の個数の最大値を出力する。 は整数 考…

No.7 プライムナンバーゲーム|yukicoder|メモ化再帰

No.7 プライムナンバーゲーム - yukicoder を 1ヶ月半 かけて解きました。 (何故 1ヶ月かかったのかは後述) 題意: ・はじめに先攻プレイヤーに自然数 N が与えられます。 ・N以下の素数のどれかで減算し相手に渡す、を先攻・後攻で交互に繰り返し、 N が 0 …

WUPC2012 A - 招待状|AtCoder

A: 招待状 - WUPC 2012 | AtCoderを解きました。 #include <bits/stdc++.h> using namespace std; struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star; int main() { vector<int> M{ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int MM1, DD1, MM2, DD2</int></bits/stdc++.h>…

SRM 602 Div2 Level-1 250:TypoCoderDiv2

SRM 602 div2 250:TypoCoderDiv2 を解きました。題意: TypoCoderには2種類の Rating がある。 Brown ➡︎ 1200以上 Ciel ➡︎ 1200以下 ただし新しい参加者は Rating 500 スタート。 N回分のコンテスト参加後の Rating が与えられるので何回色が変わったかを出…

C++で素数列挙!エラトステネスの篩 編

C++で素数列挙!O( N√N )編 を書きましたが、 今度はもっと高速な エラトステネスの篩(ふるい)編 *1 を書いたのでコードを残します。 #include <bits/stdc++.h> using namespace std; struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star; #include <vector> std::vect</vector></bits/stdc++.h>…

C++で素数列挙!O( N √N )編

以前、C++で素数判定!を書いたのですが、 今回は C++で素数列挙!O( N √N ) のコードを書いたのでメモとして残します。 #include <bits/stdc++.h> using namespace std; struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star; int N; vector<int> P; void prime( co</int></bits/stdc++.h>…

重複要素チェック|C++

std::vectorに代入された要素が全て同じ値か否かをチェックするコードを、 自分の参照用に記事として残します。 std::set は重複するデータの保持を許さないコンテナ *1 なので、 重複チェックの意味合い的にも適している、( 0 )と( 3 )を使っていこうと思い…

Round#91 A. Lucky Division|Codeforces

Problem - 122A - Codeforcesを解きました。 #include <bits/stdc++.h> #define REP(i,n) for(int i=0; i<(n); i++) using namespace std; struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star; bool check( int N ){ string S = to_string( N ); REP( i, (int</bits/stdc++.h>…

Switching Railroad Cars|AOJ|stack

stackに慣れる為に車両入れ替え | Aizu Online Judgeを解きました。 #include <bits/stdc++.h> using namespace std; struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star; int main() { int N; stack<int> st; while( cin >> N ) { if( N == 0 ){ // 0が来たら取り</int></bits/stdc++.h>…

C++ で 桁和

#include <bits/stdc++.h> using namespace std; int digit( int N ) { while( N >= 10 ) { int tmp{}; while( N > 0 ) { tmp += ( N % 10 ); N /= 10; } N = tmp; } return N; } int main() { int N{}; cin >> N; cout << digit( N ) << endl; return 0; } とある問題で桁</bits/stdc++.h>…

No.24 数当てゲーム|yukicoder

No.24 数当てゲーム - yukicoder を解きました。 #include <bits/stdc++.h> #define REP(i,n) for(int i=0; i<(n); i++) using namespace std; struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star; int main() { int N; cin >> N; vector<int> res( 10, 1 ); REP( </int></bits/stdc++.h>…

ABC007-C 幅優先探索|AtCoder

C: 幅優先探索 - AtCoder Beginner Contest 007 | AtCoder を解きました。 #include <bits/stdc++.h> #define REP(i,n) for(int i=0; i<(n); i++) using namespace std; typedef pair<int, int> PII; struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star; const int INF </int,></bits/stdc++.h>…

No.3 ビットすごろく|yukicoder|幅優先探索(BFS)

No.3 ビットすごろく - yukicoder を解きました。 #include <bits/stdc++.h> using namespace std; struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star; const int INF = numeric_limits<int>::max(); int f( int N ) { int count{}; while( N > 0 ) { if( N % 2 =</int></bits/stdc++.h>…

Round #298 A-Exam|Codeforces

Codeforces Round #298 A-Exam を解きました。 #include <bits/stdc++.h> #define REP2(i,x,n) for(int i=x; i<(n); i++) using namespace std; struct CWW{CWW(){ios::sync_with_stdio(false);cin.tie(0);}}cww; int main() { int N; cin >> N; if( N <= 2 ) { cout << 1 <</bits/stdc++.h>…

Round #326 A. Duff and Meat|Codeforces

Problem - A - Codeforces を解きました。 ktnさんに適度な問題をいくつかpick upして頂いたのですがそのうちの1問です。 (適度な問題:私のレベルに対しての適度)Duffちゃんが必要な肉の量/day と コスト/day がN日分与えられるので、 必要な肉を買う最小コ…

再帰④|トリボナッチ数列

#include <bits/stdc++.h> using namespace std; //再帰 - トリボナッチ int trib( int N ) { if( N == 0 || N == 1 )//基底部 { return 0; } else if( N == 2 )//基底部 { return 1; } else //再帰部 { return trib( N - 1 ) + trib( N - 2 ) + trib( N - 3 ); } } int mai</bits/stdc++.h>…

再帰③|フィボナッチ数列

#include <bits/stdc++.h> using namespace std; //再帰 - フィボナッチ int fib( int N ) { if( N == 0 )//基底部 { return 0; } else if( N == 1 )//基底部 { return 1; } else //再帰部 { return ( fib( N - 2 ) + fib( N - 1 ) ); } } int main() { cin.tie(0); ios::sy</bits/stdc++.h>…

再帰②|階乗

#include <bits/stdc++.h> using namespace std; //再帰 - 階乗 int sum( int N ) { if( N == 0 ) //基底部 { return 1; } else //再帰部 { return N * sum( N - 1 ); } } int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; cout << sum( N ) << end</bits/stdc++.h>…

再帰①|等差数列の和

#include <bits/stdc++.h> using namespace std; int sum( int N ) { if( N == 0 ) //基底部 { return 0; } else //再帰部 { return sum( N - 1 ) + 1; } } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; cout << sum(n) << endl; return 0; } </bits/stdc++.h>…

No.154 市バス|yukicoder

No.154 市バス - yukicoderを解きました。この問題、文字列の問題を探していて星★★だったし解けるかな〜?みたいな 軽い気持ちで適当に選んだのが全ての始まりでした...(序章)初めはstd::findで文字列を見つけて場合分けすればいけるのでは(?) みたいな感じ…

Round #130 A. Dubstep|Codeforces

Problem - 208A - Codeforcesを解きました。 #include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); string S; cin >> S; S = "WUB" + S + "WUB"; regex re( "(WUB)+" ); S = regex_replace( S, re, " " ); cout << S.subs</bits/stdc++.h>…

Round #89 A. String Task|Codeforces

Problem - A - Codeforcesを解きました。 #include <bits/stdc++.h> using namespace std; string f( string S ) { string res; for( auto&& x : S ) { //★ if( string( "oayeui" ).find( x ) != string::npos ) { continue; } res += x; } return res; } int main() { cin.</bits/stdc++.h>…

再帰と分割統治法|アルゴリズムとデータ構造

アルゴリズムとデータ構造 第6章−1「再帰と分割統治法」で階乗コードを書きました。 #include <bits/stdc++.h> #define LL long long #define ULL unsigned long long #define REP(i,n) for(int i=0; i<(n); i++) #define REP2(i,x,n) for(int i=x; i<(n); i++) using names</bits/stdc++.h>…

#65 A. Way Too Long Words|Codeforces

Codeforces Round #65 Problem - A - Codeforcesを解きました。 #include <bits/stdc++.h> #define REP(i,n) for(int i=0; i<(n); i++) #define REP2(i,x,n) for(int i=x; i<(n); i++) using namespace std; int main () { cin.tie(0); ios::sync_with_stdio(false); int N;</bits/stdc++.h>…

Old Bridges|AOJ

Old Bridges | Aizu Online Judgeを解きました。 #include <bits/stdc++.h> #define LL long long #define ULL unsigned long long #define REP(i,n) for(int i=0; i<(n); i++) #define REP2(i,x,n) for(int i=x; i<(n); i++) using namespace std; int main (){ cin.tie(0)</bits/stdc++.h>…

No.163 cAPSlOCK|yukicoder

No.163 cAPSlOCK - yukicoder を解きました。 #include <bits/stdc++.h> using namespace std; int main (){ string C; cin >> C; string S; for( char x : C ){ S += ( islower(x) == 0 ? tolower(x) : toupper(x) ); } cout << S << endl; return 0; } 似ているけれど役割</bits/stdc++.h>…

No.239 にゃんぱすー|yukicoder

No.239 にゃんぱすー - yukicoder を解きました。 #include <bits/stdc++.h> using namespace std; int main (){ int n; cin >> n; string s[100][100]; for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ cin >> s[ i ][ j ]; } } set<int> st; for(int i=0; i</int></n;></bits/stdc++.h>

No.289 数字を全て足そう|yukicoder

No.289 数字を全て足そう - yukicoder を解きました。 私の提出は見苦しいので省略。この問題は string で受け取った アルファベット + 整数 の文字列の中から 整数部分を取り出して総和を取る問題です。 string →int の変換は std::stoi() しか使った事がな…

ABC037-B 編集|AtCoder|std::fill()

B: 編集 - AtCoder Beginner Contest 037 | AtCoder を解きました。 コンテストの日に提出したコードは自分でも謎すぎて説明出来ないので割愛。 提出し直したものを...↓ #include <bits/stdc++.h> using namespace std; int main(){ cin.tie(0); ios::sync_with_stdio(false</bits/stdc++.h>…