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;
    }
    

      妙哉!

  • 相关阅读:
    okhttp之源码学习1
    Retrofit2之源码解析2
    Retrofit2之源码解析1
    retrofit之笔记内容
    retrofit之基本笔记
    retrofit之基本内容
    rxjava-源码分析
    rxjava-基本内容解析
    rxjava_几类转换
    java几种常见的编码
  • 原文地址:https://www.cnblogs.com/dragondragon/p/11194119.html
Copyright © 2011-2022 走看看