LoginSignup
1
0

More than 3 years have passed since last update.

モギモギ解説 その1

Last updated at Posted at 2020-10-07

2 番目に大きい整数 (The Second Largest Integer)

if文でかくと鬼のように難しいです。

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

int main() {
  int a,b,c;
  cin >> a >> b >> c;
  if(a <= b && b <= c) {
    cout << b << endl;
  } else if(c <= b && b <= a) {
    cout << b << endl;
  } else if(b <= a && a <= c) {
    cout << a << endl;
  } else if(c <= a && a <= b) {
    cout << a << endl;
  } else if(a <= c && c <= b) {
    cout << c << endl;
  } else if(b <= c && c <= a) {
    cout << c << endl;
  }
}

配列を使ってソートするのが一番スムーズなやり方ですね。

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

int main() {
  vector<int> a(3);
  cin >> a.at(0) >> a.at(1) >> a.at(2);
  sort(a.begin(), a.end());
  cout << a.at(1) << endl;
}

Number of Multiples

for文でもシミュレーションしてもできますが、以下のように『R以下のdの倍数の数』から『L未満のdの倍数の数』を引くことで簡単に求められます。

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

int main() {
  int L, R, d;
  cin >> L >> R >> d;
  cout << R/d - (L-1)/d << endl;
}

Buying Sweets

こちらも2番目同様にfor文でシミュレーションせずとも、以下のように『XからAを引いた値をBで割ったあまり』を求めることで答えになります。

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

int main() {
  int X, A, B;
  cin >> X >> A >> B;
  cout << (X-A) % B << endl;
}

母音を数える (Counting Vowels)

条件に合致したものを数えるのみです。

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

int main() {
  int n;
  string s;
  cin >> n >> s;

  // 答えを格納する変数
  int ans = 0;

  for(char c: s) {
    if(c == 'a' ||c == 'i' ||c == 'u' ||c == 'e' ||c == 'o') {
      // 当てはまる度にインクリメント
      ans++;
    }
  }

  cout << ans << endl;
}

JOI ソート (JOI Sort)

ちょっと難しいです。
Jの出た回数、Iの出た回数、Oの出た回数をカウントして、その後、それぞれの回数分だけそれぞれの文字を出力しましょう!

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

int main() {
  int n;
  string s;
  cin >> n >> s;
  int ansJ = 0; // Jの出た回数
  int ansO = 0; // Oの出た回数
  int ansI = 0; // Iの出た回数
  for(char c: s) {
    if(c == 'J') ansJ++;
    if(c == 'O') ansO++;
    if(c == 'I') ansI++;
  }

  // Jの出た回数だけJを表示
  for(int i=0; i<ansJ; i++) {
    cout << 'J';
  }

  // Oの出た回数だけOを表示
  for(int i=0; i<ansO; i++) {
    cout << 'O';
  }

  // Iの出た回数だけIを表示
  for(int i=0; i<ansI; i++) {
    cout << 'I';
  }
  cout << endl;
}

共通要素 (Common Elements)

これはbool型のvectorを使うことで二重for文を使わず実装可能です。

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

int main() {
  int n, m;
  cin >> n >> m;

  // bool型のvewctorを用意(初期値を指定していないので全てfalse)
  vector<bool> a(101);
  vector<bool> b(101);

  for(int i=0; i<n; i++) {
    int x;
    cin >> x;
    // 長さnの配列にxが存在することを示すためaのx番目をtrueにする。
    a.at(x) = true;
  }
  for(int i=0; i<m; i++) {
    int x;
    cin >> x;
    // 長さnの配列と長さmの配列とxが存在することを示すためaのx番目にaの中身をいれる
    b.at(x) = a.at(x);
  }

  // 長さnの配列と長さmの配列のいずれにも存在する値を小さい順に探して、見つけたら表示。
  for(int i=0; i<=100; i++) {
    if(b.at(i)) cout << i << 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