いろいろあった。
目次
1.A問題 – 宝くじ
問題:A – 宝くじ
やるだけ。if~elseはどこかでtrueになったらそれ以降は判定されないことを知っていれば書ける。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define rep(i, n) for (int i = 0; i < (n); i++) | |
#define rep2(i, s, n) for (int i = (s); i < (n); i++) | |
using namespace std; | |
using ll = long long; | |
using P = pair<int, int>; | |
int main() { | |
vector<int> e(6), l(6); | |
int b; | |
rep(i, 6) cin >> e[i]; | |
cin >> b; | |
rep(i, 6) cin >> l[i]; | |
int hit = 0; | |
bool bonus = false; | |
for (int select_num : l) { | |
for (int hit_num : e) { | |
if (select_num == hit_num) hit++; | |
if (select_num == b) bonus = true; | |
} | |
} | |
if (hit == 6) { | |
cout << 1 << endl; | |
} else if (hit == 5 && bonus) { | |
cout << 2 << endl; | |
} else if (hit == 5) { | |
cout << 3 << endl; | |
} else if (hit == 4) { | |
cout << 4 << endl; | |
} else if (hit == 3) { | |
cout << 5 << endl; | |
} else { | |
cout << 0 << endl; | |
} | |
return 0; | |
} |
2.B問題 – あみだくじ
問題:B – あみだくじ
あみだくじを逆順に辿る問題。やること自体はわかりやすい。ただ実装が面倒くさい。
一番面倒くさい点は、入力に空白があるのでcinでは1行全てを読み取れないことです。1行読み取るためにはgetlineを使います。しかし、その前にcinがある場合、その後のgetlineは前の入力の改行から読み取り始めます。これを防ぐため、ignore()を用いて前のストリームを捨てる必要があります。
あみだくじを下から辿る実装はそこまで難しくないです。私はあみだくじを隣接行列に変換して無向グラフを辿るように解いていますが、結局今の位置さえわかっていればいいので、その位置だけ常に追い続ける実装でもいいと思います。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define rep(i, n) for (int i = 0; i < (n); i++) | |
#define rep2(i, s, n) for (int i = (s); i < (n); i++) | |
using namespace std; | |
using ll = long long; | |
using P = pair<int, int>; | |
int main() { | |
//入力 | |
int n, l; | |
cin >> n >> l; | |
cin.ignore(); //これをしないと次のgetlineは改行を読むことになる | |
//あみだくじを隣接行列みたいにする | |
//無向グラフのように実装。繋がりがない場合は-1にする。 | |
vector<vector<int>> amida(l, vector<int>(n, -1)); | |
//あみだくじを隣接行列に変換 | |
rep(i, l) { | |
string s; | |
getline(cin, s); //空白を含むのでgetlineじゃないとダメ | |
for (int j = 1; j < 2 * n; j += 2) { | |
//繋がっているなら行列に書き込む | |
if (s[j] == '-') { | |
amida[i][j / 2] = j / 2 + 1; | |
amida[i][j / 2 + 1] = j / 2; | |
} | |
} | |
} | |
//あたりの位置を得る | |
int bingo_num; | |
string s; | |
getline(cin, s); | |
for (int i = 0; i < 2 * n; i += 2) { | |
if (s[i] == 'o') { | |
bingo_num = i / 2; | |
break; | |
} | |
} | |
//見ている位置の初期値は当たりの場所 | |
int now_col = bingo_num; | |
//下から辿る | |
for (int col = l - 1; col >= 0; col--) { | |
//どこにも繋がっていないなら上へ | |
if (amida[col][now_col] == -1) continue; | |
//繋がっている列があるならその列へ移動する | |
if (now_col != amida[col][now_col]) now_col = amida[col][now_col]; | |
} | |
// 1番目からなので1を足して出力する | |
cout << now_col + 1 << endl; | |
return 0; | |
} |