在头文件cmath中包含floor(type x),对x向下取整;ceil(type x),对x向上取整;
具体实现过程:(很简单,也可以自己写一个函数)
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
//要求x/y向下向上取整
int up_ceil(int x,int y){
if(x/(double)y-x/y!=0) return x/y+1;
else return x/y;
//return ceil(x/y);
}
int down_floor(int x,int y){
return x/y;
//return ceil(x/y);
}
int main(){
cout<<up_floor(3,2)<<endl;
cout<<down_floor(3,2)<<endl;
}
程序运行的结果是:
2
1
--------------------------------
Process exited after 0.4066 seconds with return value 0
请按任意键继续. . .