zoukankan      html  css  js  c++  java
  • 四舍五入取近似值

    题目描述

    写出一个程序,接受一个正浮点数值,输出该数值的近似整数值。如果小数点后数值大于等于5,向上取整;小于5,则向下取整。(也就是四舍五入啦哦!)

    my codes:

    1 #include<bits/stdc++.h>
    2 using namespace std;
    3 int main()
    4 {
    5     double n;
    6     if(n-int(n)>=0.5) cout<<int(n)+1<<endl;
    7     else cout<<int(n)<<endl;
    8     return 0;
    9 }

    A very simple way of showing 四舍五入:

    1 #include<bits/stdc++.h>
    2 using namespace std;
    3 int main()
    4 {
    5     float n;
    6     cin>>n;
    7     cout<<int(n+0.5);
    8     return 0;
    9 }

     天外有天,人外有人,用自己来见证美妙冒险!

    介绍一下一些强大的取整函数:floor(向下取整),ceil (向上取整) , round(四舍五入)。

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

      妙哉!

  • 相关阅读:
    1044 拦截导弹
    3060 抓住那头奶牛 USACO
    2727:仙岛求药(广搜)
    4906 删数问题(另一种贪心思路)
    1004 四子连棋
    1005 生日礼物
    1031 质数环
    1008 选数
    1073 家族
    2801 LOL-盖伦的蹲草计划
  • 原文地址:https://www.cnblogs.com/dragondragon/p/11194119.html
Copyright © 2011-2022 走看看