codeforces 1A 解题报告

来自Vic_:http://blog.csdn.net/Vic___


每周一道算法题安慰自己竞赛的心,囧

1A 水题一道


原题地址:http://codeforces.com/problemset/problem/1/A


问题描述

A. Theatre Square
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the size a × a.

What is the least number of flagstones needed to pave the Square? It's allowed to cover the surface larger than the Theatre Square, but the Square has to be covered. It's not allowed to break the flagstones. The sides of flagstones should be parallel to the sides of the Square.

Input

The input contains three positive integer numbers in the first line: n,  m and a (1 ≤  n, m, a ≤ 109).

Output

Write the needed number of flagstones.

Sample test(s)
input
6 6 4
output
4



题目理解

学点英语

Theatre Square     剧院广场 

rectangular        adj. 矩形的;成直角的
shape                n. 形状;模型;身材;具体化
pave                 
vt. 铺设;安排;作铺设之用
parallel             
n. 平行线;对比


我的理解

题目是要在一个n*m矩形上用a*a的正方形,平行地完全覆盖,允许重叠,但不允许裁剪,求最少正方形数

看下列图解:

左边是n*m的被覆盖矩形,右边是用来覆盖的正方形

很容易想到一种解法,就是沿着每边铺完


左边就是覆盖的铺满了,但是难点在于不好确定怎么样去覆盖,怎么确定覆盖位置
所以,用到贪心算法思想,想到一个此时的最优解
贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的仅是在某种意义上的局部最优解。

每次都沿着上一次铺过的边继续铺


左图就是这种铺法,可以得到同样结果
对此建模

很容易就能解出算法:
n/a向上取整 * m/a向上取整

解题代码

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main (){
    double n, m ,a ;
    cin>>n>>m>>a;
    cout<<fixed<<setprecision(0)<<ceil(n/a)*ceil(m/a)<<endl;
    return 0;
}

代码解释

第一次提交时候出现精度错误


果断包含iomanip文件
fixed定点格式输出
setprecision(0)设置精度为零


果断通过


粗制滥造,欢迎斧正

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值