取近似值的函数(向上,向下,四舍五入)C++

取近似值的函数(向上,向下,四舍五入)C++一 向上取整 ceil n 该值大于等于参数 并等于离该值最近的某个整数 例如 ceil 3 7 4 ceil 5 2 6 ceil 1 001 2 include bits stdc h using namespace std int main int ans bits

大家好,我是讯享网,很高兴认识大家。

一、向上取整

例如:ceil(3.7)=4      ceil(5.2)=6        ceil(1.001)=2

#include<bits/stdc++.h> using namespace std; int main() { int ans; float s; while(cin>>s) { ans=ceil(s); cout<<ans<<endl; } return 0; }

讯享网

运行结果: 


讯享网

 二、向下取整

例如:floor(3.1)=3      floor(5.9)=5        floor(1.99)=1

讯享网#include<bits/stdc++.h> using namespace std; int main() { int ans; float s; while(cin>>s) { ans=floor(s); cout<<ans<<endl; } return 0; }

运行结果: 

 三、四舍五入

例如:round(3.9)=4         round(4.2)=4         round(7.4)=7

#include<bits/stdc++.h> using namespace std; int main() { int ans; float s; while(cin>>s) { ans=round(s); cout<<ans<<endl; } return 0; }

运行结果: 

 当然了,每个函数的括号里也可以写一条语句,例如a/b,i+j等。


好了,那今天就到这里啦,感谢您的支持,有什么不懂的问题也可以写在评论区里,

我们下期再见吧(◕ᴗ◕✿),拜拜(。◕ˇ∀ˇ◕)

小讯
上一篇 2025-02-24 15:00
下一篇 2025-04-02 22:14

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/127018.html