LoginSignup
0
0

AtCoderで入茶を目指して勉強しています。
勉強を継続するために投稿を始めました。
もともとアカウントを作成していましたが、今年の4月から本格的に勉強を始めました。
一応自分用に解法を書いていますが雑です、自分で読み返して困ったら修正します。
私のアカウント
解いた問題

本日解いた問題

B - Hard Calculation

B - Hard Calculation
解答

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  ll a, b;
  cin >> a >> b;
  while(a > 0 || b > b){
    if(a%10 + b%10 >= 10) {
      cout << "Hard" << endl;
      exit(0);
    }
    a /= 10;
    b /= 10;
  }
  cout << "Easy" << endl;
}

解法

while文を使って1桁ずつ加算を考える。繰り上がったらHardを出力して終了、ループが終了したらEasyを出力。

B - Triple Metre

B - Triple Metre

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  string s, t;
  cin >> s;
  rep(i,10) t += "oxx";
  rep(i,3){
    bool jud = true;
    for(int j=0;j<s.size();j++){
      if(t[j+i] != s[j]) jud = false;
    }
    if(jud) {
      cout << "Yes" << endl;
      exit(0);
    }
  }
  cout << "No" << endl;
}

解法

oxxを10^5個つなげた文字列Tと入力された文字列Sの比較を行う。Sと比較するときTをそれぞれ1文字目、2文字目、3文字目スタートで比較するとTがSを含むかどうか判定できる。

B - Election

B - Election

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  ll n;
  map<string,ll> res;
  cin >> n;
  string s;
  rep(i,n){
    cin >> s;
    res[s]++;
  }
  ll tmp = 0;
  for(auto const&[k,v]:res){
    if(tmp<=v) {
      tmp = v;
      s = k;
    }
  }
  cout << s << endl;
}

解法

Mapを使うことで候補者の票数を簡単に数えることができる。私はMapのkeyを候補者の名前、valueを票数とした。最後にvalueが一番大きいkeyを出力する。

C - Select Mul

C - Select Mul

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  string n;
  cin >> n;
  sort(n.begin(),n.end());
  int res = 0;
  do{
    for(int i = 1; i < n.size(); i++){
      string a,b;
      for(int j = 0; j < i; j++) a += n[j];
      for(int j = i; j < n.size(); j++) b += n[j];
      if(a[0] == '0' || b[0] == '0') continue;
      res = max(res,stoi(a)*stoi(b));
    }
  } while(next_permutation(n.begin(),n.end()));
  cout << res << endl;
}

解法+メモ

順列を生成するnext_permutation関数を使用する。生成されたNの順列に対してfor文を使用し2つに分割し、乗算を行い最大値を求める。
普通にわからなかったうえにnext_permutation関数も知らなかったため要復習。解説を読んだら理解できた。

参考文献

C - Select Mul 解説 by penguinman

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