取整的方式:
头文件:#include <math.h>
-
函数
ceil(double x)
向下取整
eg:
ceil(-11.5) == -11 ceil(1.5)== 1 ceil(-6.2) == -6 ceil(6.1) == 6
-
函数
floor(double x)
向上取整
eg:
floor(-11.5) == -12 floor(1.5) == 2 floor(-6.2) == -7 floor(6.1) == 7
-
函数
round(double x)
四舍五入
eg:
round(-11.5) == -12 round(1.5) == 2 round(-6.2) == -6 round(6.1) == 6
方法一、
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float n;
cin >> n;
cout << round(n) << endl;
return 0;
}
方法二、
#include<iostream>
using namespace std;
int main()
{
double a;
cin>>a;
cout<<int(a+0.5);
return 0;
}