1 条题解

  • 0
    @ 2023-10-25 1:32:38

    【题目考点】

    1. if…else语句

    if(判表达式) {语句段1} else {语句段2} 如果判断表达式的值为true,运行语句段1。如果判断表达式的值为false,运行语句段2。

    2. 比较函数max, min(存在于< algorithm >头文件中)
    • int max(int a, int b); double max(double a, double b);返回a,b中较大的值
    • int min(int a, int b); double min(double a, double b);返回a,b中较小的值

    【解题思路】

    image

    【题解代码】

    解法1:列数学公式
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        double n, x, y;
        cin >> n >> x >> y;
        cout << max(0, (int)floor(n - y / x));//调用int max(int a, int b);求两个数的最大值 
        return 0;
    }
    
    解法2:判断不同情况

    用if…else语句

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n, x, y, rest;
        cin >> n >> x >> y;
        if (y%x == 0)
            rest = n - y/x;
        else
            rest = n - y/x - 1;
        if (rest <= 0)
            cout << 0 << endl;
        else
            cout << rest << endl;
        return 0;
    }
    
    解法3:判断不同情况

    用三目运算符 ? :

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n, x, y, res;
        cin >> n >> x >> y;
        res = n - y/x - (y%x == 0 ? 0 : 1);
        cout << (res <= 0 ? 0 : res);
        return 0;
    }
    

    信息

    ID
    613
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    (无)
    递交数
    1
    已通过
    1
    上传者