けんちょんの競プロ精進記録

競プロの精進記録や小ネタを書いていきます

AOJ 2589 North North West (JAG 模擬地区 2014 A) (200 点)

とにかく言われた通りにやる

問題へのリンク

問題概要

northnorthwest (北北西)
のように言われて、それを真北方向から何度ずれているかを既約分数で表示せよ (上の例の場合は 45/2)

考えたこと

詳細仕様が問題文中にあるので、とにかく言われた通りに頑張る。
最後に約分が必要になるが、公約数は 2 の冪乗だから 2 で割り続けるだけでいい。

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
  string S;
  while (cin >> S) {
    if (S == "#") break;
    long long deg = 0;
    int num = 0;
    reverse(S.begin(), S.end());
    for (int i = 0; i < S.size(); ++num) {
      if (S.substr(i, 5) == "htron") {
        if (i != 0) deg *= 2, --deg;
        ++n, i += 5;
      }
      else deg *= 2, ++deg, ++w, i += 4;
    }
    deg *= 90;
    long long all = 1LL<<(num-1);
    while (deg % 2 == 0 && all % 2 == 0) deg /= 2, all /= 2;
    if (all == 1) cout << deg << endl;
    else cout << deg << "/" << all << endl;
  }
}