题解

4 条题解

  • 1
    @ 2021-02-15 20:33:48
    #include<bits/stdc++.h>
    using namespace std;//这题用switch不比if好吗(光速逃
    int a,b;
    char c;
    int main()
    {
        cin>>a>>b>>c;
        switch(c){
            case '+' :cout<<a+b;break;
            case '-' :cout<<a-b;break;
            case '*' :cout<<a*b;break;
            case '/' :cout<<a/b;break;
        }
        return 0;
    }
    
  • 1
    @ 2020-07-24 15:53:02

    一个一个判断吧

    #include<bits/stdc++.h>
    using namespace std;
    
    int main()
    {
        int a, b;
        cin >> a >> b;
        char c;
        cin >> c;
        if(c == '+')
        {
            cout << a + b;
        }
        else if(c == '-')
        {
            if(a <= b)
            {
                cout << b - a;
            }
            else
            {
                cout << a - b;
            }
        }
        else if(c == '*')
        {
            cout << a * b;
        }
        else
        {
            if(a <= b)
            {
                cout << b / a;
            }
            else
            {
                cout << a / b;
            }
        }
        cout << endl;
        return 0;
    }
    
  • 1
    @ 2020-05-27 22:19:17

    没什么好讲的,水题一道。四个if语句分别判断,然后就\(ok\)了。
    代码:

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
        long long a, b;
        cin >> a >> b;
        char c;
        cin >> c;
        if (c=='+') cout << a+b << endl;
        if (c=='-') cout << a-b << endl;
        if (c=='*') cout << a*b << endl;
        if (c=='/') cout << a/b << endl;
        return 0;
    }
    
  • 0
    @ 2020-07-31 15:33:25
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int main() {
        int a, b;
        char op;
        cin >> a >> b >> op;
        switch (op) {
            case '+':
                cout << a + b;
                break;
            case '-':
                cout << a - b;
                break;
            case '*':
                cout << a * b;
                break;
            case '/':
                if (b == 0) {
                    cout << "Divided by zero!";
                } else {
                    cout << a / b;
                }
                break;
            default:
                cout << "Invalid operator!";
        }
        return 0;
    }
    
  • 1

信息

ID
1004
难度
1
分类
模拟 点击显示
标签
递交数
38
已通过
22
通过率
58%
被复制
2
上传者