题解 P2715 【ccj与zrz之在回家的路上】

· · 题解

其实我的思路和楼上楼下都差不多,

那为什么发篇题解呢

我觉得我的代码有两个可取之处

一个是对于cin省去多余符号来节省码量

另一个是边输入边处理

code


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int a, b;
char c, ch;

int main() {
    c = getchar();
    while (c == ' ')
        c = getchar();
    if (c == '?') {
        c = getchar();
        while (c == ' ')
            c = getchar();
        cin >> a >> ch >> b;
        if (c == '+') {
            printf("%.2lf\n", double(b) - double(a));
            return 0;
        }
        if (c == '-') {
            printf("%.2lf\n", double(b) + double(a));
            return 0;
        }
        if (c == '*') {
            printf("%.2lf\n", double(b) / double(a));
            return 0;
        }
        if (c == '/') {
            printf("%.2lf\n", double(b) * double(a));
            return 0;
        }
    }
    else {
        while (c >= '0' && c <= '9')
            a = a * 10 + c - '0', c = getchar();
        while (c == ' ')
            c = getchar();
        ch = c;
        c = getchar();
        while (c == ' ')
            c = getchar();
        if (c == '?') {
            cin >> c >> b;
            if (ch == '+') {
                printf("%.2lf\n", double(b) - double(a));
                return 0;
            }
            if (ch == '-') {
                printf("%.2lf\n", double(a) - double(b));
                return 0;
            }
            if (ch == '*') {
                printf("%.2lf\n", double(b) / double(a));
                return 0;
            }
            if (ch == '/') {
                printf("%.2lf\n", double(a) / double(b));
                return 0;
            }
        }
        else {
            while (c <= '9' && c >= '0')
                b = b * 10 + c - '0', c = getchar();
            if (ch == '+') {
                printf("%.2lf\n", double(a) + double(b));
                return 0;
            }
            if (ch == '-') {
                printf("%.2lf\n", double(a) - double(b));
                return 0;
            }
            if (ch == '*') {
                printf("%.2lf\n", double(a) * double(b));
                return 0;
            }
            if (ch == '/') {
                printf("%.2lf\n", double(a) / double(b));
                return 0;
            }
        }
    }
    return 0;
}

希望这篇题解能通过,

也希望大家Noip rp++

加油