【競プロ】AtCoderを解くときのTips【C++】

book-and-pc-on-desk プログラミング

AtCoderのコンテストを解く中で調べた内容を随時まとめていきます。

C++の文法については、以下の記事にまとめています。

標準入力を配列で受け取る

/*
標準入力
4
1 2 3 4 
*/

int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
  cin >> a[i];
}
// a = {1, 2, 3, 4}

文字列⇔数値の変換

//文字列⇒数値
string s = "123";
int i = stoi(s);


//数値⇒文字列
int i = 123;
string s = to_string(i);

文字列の文字を数値に変換する

string s = "123";
//文字列から一文字数値として取り出す
int i = s.at(0) - '0';  // i = 1


//使用例
//文字列sを一文字ずつ見て、その数値に対する配列vの要素を出力する。
vector<string> v = {"one", "two", "three"};
string s = "213";
for (int i = 0; i < 3; i++) {
  //配列の添字は0から始まるため -'1'
  cout << v[s[i] - '1'] << " ";  // => two one three
  //または 
  //cout << v[(s[i] - '0') -1] << " ";
}

ある文字列を含むか

ある文字列Tが文字列Sを含むか?

string t = "競技プログラミング";
string s = "プロ";

if(t.find(s) != string::npos) {
    cout << "含まれています";
} else {
    cout << "含まれていません";
}

文字列が一致した場合は、その位置を返す。見つからない場合は、「string::npos」を返す。

文字が小文字かどうか

  char c = 'c';
  if('a' <= c && c <= 'z') {
    cout << "小文字です";
  } else {
    cout << "小文字ではありません";
  }

文字が大文字かどうか

  char c = 'C';
  if('A' <= c && c <= 'Z') {
    cout << "大文字です";
  } else {
    cout << "大文字ではありません";
  }

文字列から部分文字列を取得する

substr(最初の文字の位置, 取り出す長さ)を使う。取り出す長さを指定しない場合は、文字列の最後までの部分文字列になる。

string s = "0123456789";
cout << s.substr(5) << endl;  // => 56789
cout << s.substr(0, 2) << endl;  // => 01 
cout << s.substr(3, 4) << endl;  // => 3456

配列を初期化

配列

vector<string> v(3, "#");

for (int i = 0; i < 3; i++)
{
    cout << v[i];
}

/* 出力
###
*/

二次元配列

「vector<vector<T>> 変数名(要素数1, vector<T>(要素数2, 初期値))」の初期値で宣言した値で、配列が埋められる。

vector<vector<string>> v(3, vector<string>(3, "#"));

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        cout << v[i][j];
    }
    cout << endl;
}

/* 出力
###
###
###
*/

2つの文字を入れ替える

文字列Sのa文字目とb文字目を入れ替える。

string s = "abcdef";
int a = 1;
int b = 6;

//1文字目と6文字目を入れ替え
swap(s[a-1] , s[b-1]);

cout << s;  // => fbcdea

文字列が回文か判定する

文字列Sが回文か判定する。

string s = "akasaka";

//sを反転させるための変数
string t = s;
reverse(t.begin(), t.end());

if(s == t) {
  cout << "Sは回文です";
} else {
  cout << "Sは回文ではありません";
}

xのy乗を求める

pow(x, y)を使う。

cout << pow(10, 2) << endl;  // => 100 (10の2乗)

cout << pow(2, 5) << endl;  // => 32 (2の5乗)

小数点以下の桁数を指定して表示

fixedとsetprecisionを使う。setprecision()で小数点以下の桁数を指定する。

int a = 10;
int b = 3;

//int型の計算のため、計算結果もint
cout << a / b << endl;  // => 3

//double型とint型の計算結果は、doubleになる
cout << double(a) / b << endl;  // => 3.33333

//小数点以下の桁数を10に指定
cout << fixed << setprecision(10) << double(a) / b << endl;  // => 3.3333333333

pairのネスト

  pair<string, pair<string,int>> fruit;
  fruit = make_pair("apple", make_pair("青森県", 100));
  
  cout << fruit.first << endl;  // => apple
  cout << fruit.second.first << endl;  // => 青森県
  cout << fruit.second.second << endl;   // => 100
  
  pair<string, pair<string,int>> fruit1;
  fruit1 = make_pair("orange", make_pair("和歌山県", 200));

  
  vector<pair<string, pair<string,int>>> fruits(2);
  fruits[0] = fruit;
  fruits[1] = fruit1;

  cout << fruits[1].first << endl;   // => orange
  cout << fruits[1].second.first << endl;   // => 和歌山県
  cout << fruits[1].second.second << endl;   // => 200
タイトルとURLをコピーしました