gpt4 book ai didi

c - 找出铺设给定尺寸的地板所需的部分瓷砖数量

转载 作者:太空宇宙 更新时间:2023-11-04 00:00:36 25 4
gpt4 key购买 nike

这个程序必须计算铺地板需要多少 block 瓷砖。瓷砖为 8 英寸 x 8 英寸。瓷砖可以作为一个整体使用,也可以使用瓷砖的一部分。一 block 瓷砖只能切出一 block 可用的部分。也就是说,如果从瓷砖上切下一 block ,则必须将瓷砖的其余部分扔掉。该程序接受房间的长度和宽度,并返回使用了多少整 block 瓷砖以及使用了多少部分瓷砖。长度以英寸为单位。

我已经尝试过这个问题并且在获得所需的完整图 block 数量方面没有问题,但我似乎无法获得所需的部分图 block 数量。我为此编写了这个程序。

#include<stdio.h> 
int main()
{
int l,b,full,l1,bl,part;
float ar2,ar3;
scanf("%d%d,&l,&b);
ar3=(float)(l*b)/64;
l1=l/8;
bl=b/8;
full=l1*bl;
ar2=ar3-full;
part=ar2*2;
printf("%d\n%d",full,part);
return 0;
}

最佳答案

常见的方法是让瓷砖与墙壁平行。在这种情况下,您只需找出两个方向所需的瓷砖数量,瓷砖的总数就是这两个数字的乘积。获取部分图 block 数量的技巧是分别计算整个图 block 的数量,然后计算整个部分图 block 的数量。代码变为:

#include<stdio.h>

int main()
{
int l,b,full,l1,bl,part;
int partl = 0, partb = 0;

scanf("%d%d",&l,&b);

l1=l/8; // number of whole tiles
if ((l % 8) != 0) {
partl = 1; // will need one partial at the end
}
// second direction
bl=b/8;
if ((b % 8) != 0) {
partb = 1; // will need one partial at the end
}
full=l1*bl; // total number of whole tiles
if (partl) l1 += 1; // total (including partial) per direction
if (partb) bl += 1;
part = l1 * bl - full; // partial is total - number of whole tiles
printf("%d\n%d",full,part);
return 0;
}

关于c - 找出铺设给定尺寸的地板所需的部分瓷砖数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46543090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com