2017-08-24 15:42:30
writer: pprp
感觉自己好菜啊,这个题都没有做的很好
题意很简单,用a * a 的地砖,将 n * m 的地板铺满,问最少需要多少个地砖?
一开始打算分情况讨论,恰好铺满某一行,某一列,分了很多种情况,(贪心的去选择)
但是其实根本没有必要那么做,用向上取整函数就可以搞定了,也很容易理解
但是这个题其实在数据比较大的时候会溢出,所以用long long
在数据比较长的时候用cout可能会出现1e n的形式不满足要求
所以还是要补充一下
1、floor & ceil的知识点
2、cout 的精度设置
1、floor & ceil
都是在math.h的头文件下的函数
函数形式: double floor( double x )向下取整
double ceil ( double x ) 向上取整
注意返回类型是double
2、cout的其他函数及用法
在iomanip头文件下的函数
setprecition(n)对浮点数类型设置精度
std :: fixed 将整数类型的以整数的形式输出
std :: scientific 将整数以科学计数法形式输出(与fixed相对性)
代码如下:
/* theme:铺地砖 writer:pprp declare:虽然很简单,但是依然没有做的很好, 里面cout的有些知识点没有把握清楚, 还有ceil的用法也不是很清楚 date:2017/8/23 */ #include <bits/stdc++.h> #include <iomanip> using namespace std; typedef long long ll; int main() { ll width, height; double a; // scanf("%lld%lld%lld",&width,&height,&a); srand((int)time(NULL)); for(int i = 0 ; i < 10 ; i++) { srand((int)time(NULL)+i); width = rand() % 100; height = rand() % 100; a = rand() % 100; cout << width <<" " << height << " " << a << endl; //如果将width和height设为ll就用这种方法 // printf("%lf\n",(width/a + (width%a == 0 ? 0 : 1)) * (height/a + (height%a == 0 ? 0:1))); //或者采用向上取整的方法,涉及到cout用法 printf("%lf\n",ceil(width/a) * ceil(height/a)); cout<<fixed<<setprecision(0)<<ceil(width/a)*ceil(height/a)<<endl; } return 0; }