求助

P1422 小玉家的电费

```cpp #include<iostream> #include <iomanip> using namespace std; int main() { int n,x,y; cin>>n; if(n<=150){ cout<<n*0.4463<<endl;} if( n>150 && n<=400) {x=n-150; cout<<setiosflags(ios::fixed)<<setprecision(1)<<x*0.4663+66.945<<endl;} if(n>400) {y=n-400; cout<<setiosflags(ios::fixed)<<setprecision(1)<<y*0.5663+183.52 <<endl;} return 0; } ```
by 空kong @ 2020-05-21 12:39:18


``` if(n<=150){ cout<<n*0.4463<<endl;} ``` 保留一位小数呢?
by Trinitrotoluene @ 2020-05-21 12:40:27


@[木子马乔](/user/346355) 建议用printf函数; 需要cstdio头文件 保留一位小数的标准写法: ```cpp printf("%.1lf",你想输出的数); ```
by k3v1n070828 @ 2020-05-21 12:43:57


感谢dalao
by 空kong @ 2020-05-21 12:48:35


```cpp #include<bits/stdc++.h> using namespace std; int main(){ long long a; long double b; cin>>a; if(a<=150){ b=a*0.4463; }else if(a>=151&&a<=400){ b=150*0.4463+(a-150)*0.4663; }else{ b=150*0.4463+250*0.4663+(a-400)*0.5663; } cout<<fixed<<setprecision(1)<<b; return 0; } ```
by zhouzhiyuan @ 2020-06-06 15:38:27


|