C++ メモ
リストの実装
# 2021/03/15
# include <iostream>
# include <list>
using namespace std;
int main(void){
list<int> x;
for(int i=0;i<10;i++){
x.push_back(i*3);
}
for(auto i = x.begin(); i != x.end(); i++){
cout << *i << endl;
}
// 0
// 3
// 6
// ...
// 27
}
・最小公倍数(lcm)、最大公約数(gcd)
# include <iostream>
# include <numeric>
using namespace std;
int main(){
cout << lcm(9,12) << endl;
// 36
cout << gcd(49,14) << endl;
// 7
}
・next_permutation(順列全探索)(2021/05/12 : 色々間違ってたので訂正)
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
int main(void){
vector<int> a = {2,4,6};
do{
cout << a[0] << a[1] << a[2] << endl;
}while(next_permutation(a.begin(), a.end()));
}
// 246
// 264
// 426
// 462
// 624
// 642
・文字列中の文字をカウント
# include <iostream>
using namespace std;
int main(void){
string s = "atcoder_beginner_contest";
cout << count(s.cbegin(),s.cend(),'e') << " ";
cout << count(s.cbegin(),s.cend(),'t') << endl;
// 4 3
}
・繰り返しニ乗法
long long modPow (long long x, long long y, long long MOD) {
if (y == 0) return 1;
if (y % 2 == 1) return (x * modPow(x, y-1, MOD)) % MOD;
long long xx = modPow(x, y/2, MOD);
return (xx * xx) % MOD;
}
この記事が気に入ったらサポートをしてみませんか?