C++,为什么第三个数据不出 All 而出 Bike 啊x

B2046 骑车与走路

318cyz @ 2023-10-06 19:19:30

#include <iostream>

using namespace std;

int main(){

    int a;
    cin >> a;
    if (a / 3 + 50 == a / 1.2){
        cout << "All" << endl;
    }
    else if (a / 3 + 50 > a / 1.2){
        cout << "Walk" << endl;
    }
    else {
        cout << "Bike" << endl;
    }

 return 0;
}

by hexuchen @ 2023-10-06 19:21:32

@318cyz 把int改成double

#include <iostream>

using namespace std;

int main(){

    double a;
    cin >> a;
    if ((a / 3 + 50) == (a / 1.2)){
        cout << "All" << endl;
    }
    else if (a / 3 + 50 > a / 1.2){
        cout << "Walk" << endl;
    }
    else {
        cout << "Bike" << endl;
    }

 return 0;
}

by hexuchen @ 2023-10-06 19:22:09

@318cyz 求关注~


by forever_nope @ 2023-10-06 19:31:05

改成 a / 3.0 也行吧

不确定哈,你试试

我一般确实也是直接写 double 了


by 318cyz @ 2023-10-06 19:36:54

@hexuchen Done.


by Hatsunatsu @ 2023-10-06 19:38:06

用浮点数类型@318cyz


by 318cyz @ 2023-10-06 19:40:11

@RainPPR @hexuchen 谢谢x

成功 A 掉了

看来还是只要有一个是 float 的话就都用 float 比较保险(?


by caizengyu @ 2024-01-14 12:46:09

@318cyz 用double


by edu1081457001 @ 2024-02-15 15:18:56

double 就可以了


by chen_1111 @ 2024-06-07 22:03:55

int->double

AC代码:

#include<iostream>
using namespace std;
int main()
{
    double n,a,b;
    cin>>n;
    a=n/1.2;
    b=n/3.0+50;
    if(a<b)
        cout<<"Walk";
    else if(a>b)
        cout<<"Bike";
    else
        cout<<"All";
    return 0;
}

|