C++

ABC033_B

pairについて この問題では結局使わなかったけど調べたので書いとく. //市の名前と人口を入力 string s; int x; vector<pair<string, int> > city; for(int i=0;i<n;i++){ cin>>s>>x; city.push_back(make_pair(s,x)); } vector<pair<string, int>> city; int>>はint> > と書かないとcompilation errorを吐く.</pair<string,></n;i++){></pair<string,>

ABC035_B

文字列のカウント string str = "a1a2a3a4a5a6"; int cnt = count(str.begin(),str.end(),'a');//6 #include <bits/stdc++.h> using namespace std; struct Initializer { Initializer() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(15); } } in</bits/stdc++.h>…

ABC039_B

文字列を右90度に回転させる問題 #include <bits/stdc++.h> using namespace std; static const int MAX_N = 50; int main() { char a[MAX_N][MAX_N+1]; char b[MAX_N][MAX_N+1]; int n; cin>>n; //input for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cin>>a[i][j]; } } //回転 for(int i=0;i</n;i++){></bits/stdc++.h>

ABC048_B

a以上b以下の数字のうちxで割り切れるものの数を答える. for文でループで解くのは一重ループだとしてもb = 10^18とか与えられるとキツイ. 上図で赤はxで割り切れる数 上段 = 中段 - 下段で考えた. aがxで割り切れた場合はaの1つ分を足す必要がある. #include <bits/stdc++.h></bits/stdc++.h>…

ABC051_B

counter変数の加算で右辺に条件式を持ってきてもよいことがわかった. ex) int cnt = 0; cnt += 0<=k && k<=10; //kが0<=k<=10ならばTrue=1が加算され,そうでなければFalse=0が加算される. #include <bits/stdc++.h> using namespace std; int main() { int k,s; int x,y,z; </bits/stdc++.h>…

便利ワザ2

定数 const int N = 1000; const int inf = (int)1e9 + 1; const ll big = (ll)1e18 + 1; const int P = 31; const int MOD = (int)1e9 + 7; const int MOD1 = (int)1e9 + 9; const int MAX_INT = (1 << 31) - 1; const double eps = 1e-6; const double pi …

ABC053_B

文字列反転の問題 文字列反転は次のようにする. string str = "abcde"; reverse(str.begin(),str.end());//"edcba" #include <bits/stdc++.h> using namespace std; int main() { string str; cin>>str; int start=0,end=0; for(int i=0;i</bits/stdc++.h>

ABC055_B

factorialを求める問題 大きい数値はll型を使う. int型(4byte)の範囲は20億くらいを目安にする. #include <bits/stdc++.h> using namespace std; #define ll long long int #define MOD (ll)1e9+7 #define MAX(X,Y) ((X)>(Y)?(X):(Y)) #define MIN(X,Y) ((X)<(Y)?(X):(Y)) i</bits/stdc++.h>…

ABC025_A

文字列についての問題 文字列の要素1つはchar型だと知った. ex) string str = "abcde"; cout<<typeid(str[1]).name()<<endl; //c よってsubstr(ポインタの位置,そこから何文字分?)を使って部分文字列を使った. #include <bits/stdc++.h> using namespace std; int main() { string s; int n; vector<string> ss; cin>>s>>n; sort(s.begin(),s.end()); for(int i=0;i<5;i++){ for(int…</string></typeid(str[1]).name()<<endl;>

便利ワザ

入力に対して出力を行うのをプログラムがやってくれる. mainの後ろに置く. // ios_base::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif

ABC046_A

種類を数える問題 集合(set)を用いる #include <bits/stdc++.h> using namespace std; int main() { int a,b,c; set<int> s; cin>>a>>b>>c; s.insert(a);//集合sにaを挿入 s.insert(b); s.insert(c); cout<</int></bits/stdc++.h>

標準ライブラリ一括インクルード

#include <bits/stdc++.h></bits/stdc++.h>

ABC050_A

文字列を分割して数値にする問題C++におけるsplit関数の実装 qiita.com #include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> using namespace std; int stoi(std::string str){ int ret; std::stringstream ss; ss<<str; ss>>ret; return ret; } vector<string> split(const s</string></str;></vector></sstream></string></cstdio></iostream>…

ABC051_A

文字列の置換 www.geocities.jp #include<iostream> #include<string> using namespace std; // 文字列を置換する std::string Replace(std::string String, std::string From, std::string To ) { std::string::size_type Pos( String.find( From ) ); while( Pos != std::str</string></iostream>…

AOJ0087 Strange Mathematical Expression

逆ポーランド記法の問題 stackを用いる. gist.github.com C++で一行入力をするには string s,tmp; getline(cin,s);//文字列sに一行入力 //cin.eof()空だったらtrue stringstream ss(s); while(ss >> tmp){ //空白で区切られたモノに分割する //一つの単位がt…

AOJ0061 Rank Checker

チームの順位 | Aizu Online Judge AIZU ONLINE JUDGE: Code Review AIZU ONLINE JUDGE: Code Review

AOJ0051 Differential II

最大の整数と最小の整数の差 | Aizu Online Judge // gist.github.com stoll(string)はC++14 sort(),reverse()は文字列でも可能

AOJ0049 Blood Groups

血液型の分類 | Aizu Online Judge // gist.github.com int num; char comma; string s; cin>>num>>commma>>s; で違う方の一行入力を型ごとに変数に格納出来る. 入力が無い時はif(cin.eof())break;で終わらせる事ができる. string のi番目から始めたい時はst…

AOJ0038 Poker Hand

ポーカー | Aizu Online Judge 場合分けを頑張る // gist.github.com

AOJ0033 Balls

玉 | Aizu Online Judge 再帰 // gist.github.com // gist.github.com AIZU ONLINE JUDGE: Code Review

AOJ0018 Sorting Five Numbers

降順ソートの方法 sort(a,a+N); reverse(a,a+N); だと一手間多くなってしまうので sort(a,a+N,std::greater<int>()); とすると降順ソートが出来る.</int>