LoginSignup
1
0

More than 3 years have passed since last update.

モギモギ 3rd 解説

Last updated at Posted at 2020-10-15

A - Apple Pie

#include <bits/stdc++.h>
using namespace std;

int main() {
  int a, p;
  cin >> a >> p;
  cout << (a*3 + p) / 2 << endl;
}

A - すぬけそだて――登録――

#include <bits/stdc++.h>
using namespace std;

int main() {
  int a, b;
  string s;
  cin >> a >> b >> s;
  if(a <= s.size() && s.size() <= b) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}

A - 加算王

#include <bits/stdc++.h>
using namespace std;

int main() {
  int x;
  cin >> x;
  // 10で割った結果が2桁目
  // 10で割った余りが1桁目
  cout << x/10 + x%10 << endl;
}

B - Palindromic Numbers

#include <bits/stdc++.h>
using namespace std;

int main() {
  int a, b;
  cin >> a >> b;

  int ans = 0;
  for(int i = a; i <= b; i++) {
    int digit1 = i / 10000;      // 10000の桁の値
    int digit2 = i / 1000 % 10;  // 1000の桁の値
    int digit4 = i / 10 % 10;    // 10の桁の値
    int digit5 = i % 10;         // 1の桁の値

    // 10000桁と1桁が同じで1000桁と10桁が一緒なら回文数。
    if(digit1 == digit5 && digit2 == digit4) {
      ans++;
    }
  }

  cout << ans << endl;
}

B - Bingo

#include <bits/stdc++.h>
using namespace std;

int main() {
  vector<vector<int>> a(3, vector<int>(3));
  for(int i = 0; i < 3; i++) {
    for(int j = 0; j < 3; j++) {
      cin >> a.at(i).at(j);
    }
  }

  int n;
  cin >> n;

  // 各マスの穴が空いているのかの情報を管理する配列
  vector<vector<bool>> result(3, vector<bool>(3));
  for(int i = 0; i < n; i++) {
    int b;
    cin >> b;

    // b を入力するたびにその値と同値のマスがある場所をtrueにする。
    for(int j = 0; j < 3; j++) {
      for(int k = 0; k < 3; k++) {
        if(a.at(j).at(k) == b) {
          result.at(k).at(j) = true;
        }
      }
    }
  }

  // 出力結果を格納する文字列変数
  string ans = "No";

  for(int i = 0; i < 3; i++) {
    bool horizontal = true;  // 水平方向にビンゴがあるかどうか?
    bool vertical = true;    // 垂直方向にビンゴがあるかどうか?

    for(int j = 0; j < 3; j++) {
      // それぞれの方向に1つでも空いてないマスがあれば false にする。
      if(!result.at(i).at(j)) horizontal = false;
      if(!result.at(j).at(i)) vertical = false;
    }

    // 水平方向か垂直方向にビンゴがあれば、
    // 答えを "Yes" にして、ループを抜ける。
    if(horizontal || vertical) {
      ans = "Yes";
      break;
    }
  }

  // 斜め方向にビンゴがあるかを確認。
  if(result.at(0).at(0) && result.at(1).at(1) && result.at(2).at(2)) ans = "Yes";
  if(result.at(0).at(2) && result.at(1).at(1) && result.at(2).at(0)) ans = "Yes";

  cout << ans << endl;
}

B - Frog 2

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  for(int i = 0; i < n; i++) cin >> h.at(i);

  // dpはスタートから各地点までの最短コストを格納した配列。
  // 初期値を限りなく無限に近い巨大数にする。
  vector<long long> dp(n, 10000000000);
  dp.at(0) = 0;

  for(int i = 1; i < n; i++) {
    for(int j = 1; j <= k && i-j >= 0; j++) {
      // 前提条件:
      // スタートから i-j地点 を通って i地点 に行く時の最小コスト経路のコストは
      // (i-j地点 から i地点 までは一度のジャンプで行くものとする)
      // スタートから i地点 までの最小コスト( dp.at(i-j) ) と
      // i地点 から i-j地点 に移動するのに必要なコスト( abs(h.at(i)-h.at(i-j)) ) を
      // 足した結果である
      // dp.at(i-j) + abs(h.at(i)-h.at(i-j))

      // このfor文では i-k地点 から i-1地点 までの各地点を通り、
      // i地点にいくまでの最小コストの経路はの中から最小のものを探す。
      dp.at(i) = min(dp.at(i), dp.at(i-j) + abs(h.at(i)-h.at(i-j)));
    }
  }

  cout << dp.at(n-1) << endl;
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0