yukicoder no.221/222/223 参加

初yukicoder参加。

  1. No.221 犯罪都市 - yukicoder
  2. No.222 引き算と足し算 - yukicoder
  3. No.223 1マス指定の魔方陣 - yukicoder

1,2はただの実装。3は分からなくてそのまま時間切れとなってしまった。

犯罪都市

int main() {
    int N; cin >> N;
    N *= 100;
    double total = 1000*1000;
    double normal = total - N;
    double arrested = N * 0.99 + normal * 0.01;
    cout << normal / arrested << endl;
    return 0;
}

引き算と足し算

字句解析のように実装したが、yukicoderはLL使えるようなのでそっちでやりゃよかった...。こういう問題初めてなので謎にC++で頑張ってしまった。

int main() {
    string s;
    cin >> s;
    string x="", y="";
    int i = 0;
    if (s[i] == '-'){
        x+='-';
        i++;
    }
    if (s[i] == '+'){
        i++;
    }
    while (s[i] != '-' && s[i] != '+'){
        x += s[i];
        i++;
    }
    char op = s[i];
    i++;
    if (s[i] == '-'){
        y+='-';
        i++;
    }
    if (s[i] == '+') {
        i++;
    }
    while (i < s.size()) {
        y += s[i];
        i++;
    }
    stringstream ss(x);
    int xi; ss >> xi;
    stringstream ss2(y);
    int yi; ss2 >> yi;
    if (op == '-') cout << xi + yi << endl;
    else  if (op == '+') cout << xi - yi << endl;
    return 0;
}