列表

详情


WY16. 不要二

描述

二货小易有一个W*H的网格盒子,网格的行编号为0~H-1,网格的列编号为0~W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。
对于两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根
小易想知道最多可以放多少块蛋糕在网格盒子里。

输入描述

每组数组包含网格长宽W,H,用空格分割.(1 ≤ W、H ≤ 1000)

输出描述

输出一个最多可以放的蛋糕数

示例1

输入:

3 2

输出:

4

原站题解

C 解法, 执行用时: 2ms, 内存消耗: 228KB, 提交时间: 2019-01-01

#include <stdio.h>

int ls(int w,int c);
int main()
{
    int w,h,i,j=0;
    scanf("%d %d\n",&w,&h);
    for(i=0;i<h;i++)
    {
        j=j+ls(w,i);
    }
    printf("%d ",j);
    
}
int ls(int w,int c)
{    
    int i;
    if(c%4==0 || c%4==1)
    {    if(w%4==0)
                i=w/4*2;
            else if(w%4==1)
                i=w/4*2+1;
            else 
                i=w/4*2+2;
     }
     else
     {
         if(w%4==1 || w%4==2)
                i=w/4*2;
            else if(w%4==3)
                i=w/4*2+1;
            else 
                i=w/4*2;
     }
 return i;
}

C 解法, 执行用时: 2ms, 内存消耗: 236KB, 提交时间: 2018-12-17

#include<stdio.h>
int main()
{
    int div_r, div_c, remaind_r, remaind_c;
    int w, h;
    int sum;
    int num = 0;
    int row1, row2, col1, col2;
    int r, c;
    scanf("%d %d",&r, &c);
    div_r = r / 4;
    div_c = c / 4;
    remaind_r = r % 4;
    remaind_c = c % 4;
    if(remaind_r == 0)
    {
        num = 0;
    }
    else if(remaind_r == 1)
    {
        if(remaind_c == 0)
            num = 0;
        if(remaind_c == 1)
            num = 1;
        if(remaind_c == 2 || remaind_c == 3)
            num = 2;
    }
    else if(remaind_r == 2)
    {
        if(remaind_c == 0)
            num = 0;
        if(remaind_c == 1)
            num = 2;
        if(remaind_c == 2 || remaind_c == 3)
            num = 4;
    }
    else if(remaind_r == 3)
    {
        if(remaind_c == 0)
            num = 0;
        if(remaind_c == 1)
            num = 2;
        if(remaind_c == 2)
            num = 4;
        if(remaind_c == 3)
            num = 5;
        if(remaind_c == 4)
            num = 6;
    }
    else if(remaind_r == 4)
    {
        if(remaind_c == 0)
            num = 0;
        if(remaind_c == 1)
            num = 2;
        if(remaind_c == 2)
            num = 4;
        if(remaind_c == 3)
            num = 6;
        if(remaind_c == 4)
            num = 8;
    }
    sum = 8 * div_r * div_c + div_r * remaind_c * 2 + div_c * remaind_r * 2 + num;
    printf("%d", sum);
}

上一题