40求调

B2016 浮点数向零舍入

HEROBRINEH @ 2024-09-22 10:59:28


#include<bits/stdc++.h>
using namespace std;
int main(){
    float f;
    cin>>f;
    int a;
    if(f>=0) a=f;
    else a=f+1;
    cout<<a;
    return 0;
}

by furina_1013 @ 2024-09-22 20:38:44

不知道哪里出错了诶


by furina_1013 @ 2024-09-22 20:39:06

改了改,最多80

#include<bits/stdc++.h>
using namespace std;
int main(){
    double f;
    cin>>f;
    int a;
    if(f>0) a=f;
    else if(f<0)
    {
        a=--f;
        a++;
    }
    else if(f==0)a=0;
    cout<<a;
    return 0;
}

by furina_1013 @ 2024-09-22 20:40:16

@HEROBRINEH 原来能得40纯属巧合,负数写的是错的,可以先--f,再复制给a 最后a++


by furina_1013 @ 2024-09-22 20:44:20

建议这样写:```cpp if (f>0) { a= floor(f); } else if (f<0) { a=ceil(f); } else if(f==0){ a=f; } cout<<a;


by furina_1013 @ 2024-09-22 20:45:19

建议这样写:

if (f>0) { a= floor(f); } else if (f<0) { a=ceil(f); } else if(f==0){ a=f; } cout<<a;

by HEROBRINEH @ 2024-09-24 22:01:24

@furina_1013 谢谢大佬


by furina_1013 @ 2024-09-24 22:06:45

@HEROBRINEH 求关awa


by HEROBRINEH @ 2024-09-26 21:24:03

@furina_1013 已关


by furina_1013 @ 2024-09-26 21:36:12

@HEROBRINEH 感谢


|