C++40分求助

B2046 骑车与走路

name_x @ 2023-05-16 22:25:23

不知道错哪里???

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int bike=50,walk=0;
    int km;
    cin>>km;
    km*=1000;
    if((km/3+bike)>(km/1.2))
    {
        cout<<"Bike";
    }
    else if((km/3+bike)<(km/1.2))
    {
        cout<<"Walk";
    }
    else
    {
        cout<<"All";
    }
    return 0;
}

by devdede @ 2023-05-16 22:47:29

  1. 这里应该用double存精度而不是int
  2. km没有必要 \times 1000
  3. 建议使用变量来存贮 bike walk
#include<bits/stdc++.h>
using namespace std;
double bike=50.0,walk=0.0;
int main()
{

    double km;
    cin>>km;

    walk=km/1.2;
    bike=km/3.0+50.0;

    if(walk>bike)
        cout<<"Bike";
    else if(bike>walk)
        cout<<"Walk";
    else
        cout<<"All";
    return 0;
}

by name_x @ 2023-05-17 20:35:34

谢谢谢谢,看的有点晚,感激不尽感激不尽!


|