LoginSignup
0
0

More than 3 years have passed since last update.

部内バチャ解説 2020.09.11

Last updated at Posted at 2020-09-11

B - Uneven Numbers

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

int main(){
  // 入力部
  int n;
  cin >> n;

  // 桁数が奇数であるようなものの個数をカウントする変数
  int ans=0;

  // 1 ~ N までの全ての数字で全探索
  for(int i = 1; i <= n; i++){

    // cp: 現在のiの値(判定したい数値)をコピー
    // digit: 桁数をカウントするための変数
    int cp=i, digit=0;

    // cp(iのコピー) が 0になるまで 10 で割るをを繰り返す。
    // 10で割るたびにcpに1を加算して桁数を数えていく。
    while(cp){
      digit++;
      cp /= 10;
    }

    // 桁数が2で割り切れない場合、最終的な答えに1を加算する。
    if(digit % 2) ans++;
  }
  cout << ans << endl;
}

B - ATCoder

イメージで言うと音ゲーとかの『ゲーム終了時の最大コンボ数』を出力するイメージが近いです。

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

int main() {
  // 入力部
  string s;
  cin >> s;

  // ans: 最も長い ACGT 文字列 の長さをカウントする
  // now: 連続で 'a', 'c', 'g', 't' があった回数をカウントする。
  //      それ以外の文字が途中に入った場合は0に戻す。
  int ans = 0, now = 0;

  // 文字列回数分だけループする。
  for(int i=0; i<s.size(); i++) {

    // 確認している文字が 'a', 'c', 'g', 't' であった場合は now に 1を加算。
    // それ以外なら now に 0 に戻す。
    if(s.at(i) == 'A' || s.at(i) == 'C' || s.at(i) == 'G' || s.at(i) == 'T') {
      now++;
    } else {
      now = 0;
    }

    // ansに『今までの最長のACGT文字列の長さ』と『現在の連続したAGCT文字列の長さ』を比較して長い方を格納する。
    ans = max(ans, now);
  }
  cout << ans << endl;
}

B - Grid Compression

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

int main() {
  int H, W;
  cin >> H >> W;

  vector<vector<char>> vec(H, vector<char>(W)); // H x W のchar型の二次元配列

  vector<bool> vecH(H, false); // i行目に#があるならtrue, ないならfalse
  vector<bool> vecW(W, false); // j列目に#があるならtrue, ないならfalse


  for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
          // char型の二次元配列に文字を入力
          cin >> vec.at(i).at(j);

          // '#' があるのであれば、その行と列のフラグを true にする。
          if(vec.at(i).at(j) == '#') {
              vecH.at(i) = true;
              vecW.at(j) = true;
          }

      }
  }

  for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
          // 参照してるマスの列と行のフラグがどちらもtrueならその文字のマスの文字を出力する。
          // vecH.at(i) が true なのは、i行目に '#' が存在することを示す。
          // vecW.at(i) が true なのは、i列目に '#' が存在することを示す。
          if(vecH.at(i) && vecW.at(j)) {
              cout << vec.at(i).at(j);
          }
      }
      // i行目のフラグがtrue(i行目に'#'が一つ以上存在する)のであれば改行する。
      // falseの場合に改行すると、何も表示していない行なのに改行しています。
      if(vecH.at(i)) cout << endl;
  }
}

B - Sum of Three Integers

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

int main() {
  // 入力部
  int k, s;
  cin >> k >> s;

  // ansは、条件を満たす x, y, z の組み合わせの数
  int ans = 0;

  // x と y の あり得る全てのパターンを試す。
  for(int x = 0; x <= k; x++) {
    for(int y = 0; y <= k; y++) {
      // x と y が定まれば s = x + y + z を満たす z は一意に定まるので以下の通り。
      int z = s - x - y;

      // 求まった z が0以上9以下か判定
      if(0 <= z && z <= k) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}

0
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
0
0