lwj200623 @ 2024-09-02 13:16:47
#include<bits/stdc++.h>
using namespace std;
int main()
{
int w;
double a;
cin >> w;
cout << fixed << setprecision(1);
if (w <= 150)
{
a = w * 0.4463;
}
else if (150 < w <= 400)
{
a = (150 * 0.4463 + (w - 150) * 0.4663);
}
else if (w>400)
{
a = 150 * 0.4463 + 250 * 0.4663 + (w - 400) * 0.5663;
}
a = int((a * 10) + 0.5) / 10.0;
cout << a;
}
by haimingbei @ 2024-09-02 13:24:19
@lwj200623 第14行的150 < w <= 400
是不能这样写的,跟数学里有点区别,不能直接这样比较,要用 逻辑与:&&
,表示并且的意思,所以应该写成150 < w && w <= 400
by lwj200623 @ 2024-09-02 13:26:36
@haimingbei 感谢!
by haimingbei @ 2024-09-02 13:27:36
@lwj200623 如果没学的话可以问问老师或者上网查询,这是很常用且基本的逻辑运算
by hanxiaofensheng @ 2024-09-02 14:20:10
#include<bits/stdc++.h>
using namespace std;
int n;
double ans;
int main(){
cin>> n;
if(n<=150) ans=n*0.4463;
else if(n<=400) ans=150*0.4463+(n-150)*0.4663;
else ans=150*0.4463+250*0.4663+(n-400)*0.5663;
printf("%.1lf",ans);
return 0;
}
温馨提示,如果衣服或者else后面的内容只有一行,可以不用加括号的。。。。。。 @lwj200623
by hanxiaofensheng @ 2024-09-02 14:20:45
嘿嘿,不小心打错了是if @lwj200623
by hanxiaofensheng @ 2024-09-02 14:21:35
用printf更简单 @lwj200623
by lwj200623 @ 2024-09-02 15:42:51
@xiaokeai1234 不会啊
by fifast @ 2024-09-04 21:14:06
@xiaokeai1234 n和ans为什么要写成全局变量?
by fifast @ 2024-09-04 21:21:35
@xiaokeai1234 全局变量不能改成局部变量吗?
by hanxiaofensheng @ 2024-09-05 09:49:45
@fifast 这是习惯