HDU-正方形棋盘覆盖问题

79 篇文章 0 订阅
问题及代码:
/*  
*Copyright (c)2014,烟台大学计算机与控制工程学院  
*All rights reserved.  
*文件名称:HDU.cpp  
*作    者:单昕昕  
*完成日期:2015年1月27日  
*版 本 号:v1.0  
*问题描述:Consider the problem of tiling an n×n chessboard by polyomino pieces that are k×1 in size; Every one of the k pieces of each polyomino tile must align exactly with one of the chessboard squares. Your task is to figure out the maximum number of chessboard squares tiled. 
*程序输入:There are multiple test cases in the input file.
First line contain the number of cases T ($T \leq 10000$). 
In the next T lines contain T cases , Each case has two integers n and k. ($1 \leq n, k \leq 100$) 
*程序输出:Print the maximum number of chessboard squares tiled. 
Sample Input
2
6 3
5 3
Sample Output
36
24
*/
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        if(n<k)
        {
            printf("0\n");
            continue;
        }
        int r=n%k;
        if(r<=k/2)
            cout<<n*n-r*r<<endl;
        else
            cout<<n*n-(k-r)*(k-r)<<endl;
    }
    return 0;
}



运行结果:


知识点总结:
给一个n*n的正方形棋盘,由kX1的小块组成,问用这个小的(k*1)的小块来覆盖棋盘,最多能占多少格(每格是一个1X1的小格);
有个结论:当n%k!=0时,最多为:n*n-min(a,b);其中a=(n%k)*(n%k),b=(k-n%k)*(k-n%k);
当n%k==0时,显然是n*n;

学习心得:

找规律。。不要挣扎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值