C 程序用于查找通过隧道的体积
cserver side programmingprogramming
假设有一条隧道,其高度为 41,宽度非常大。我们还有一个包含长度、宽度和高度的箱子列表。如果箱子的高度恰好小于隧道高度,则箱子可以通过隧道。我们必须找到通过隧道的体积。体积为长度 * 宽度 * 高度。因此,我们有一个数字 N,一个具有 N 行和三列的二维数组。
因此,如果输入为 N = 4 个盒子 = [[9,5,20],[3,7,15],[8,15,41],[6,3,42]],则输出将为 900 和 315,我们可以传递前两个盒子,体积为 9 * 5 * 20 = 900 和 3 * 7 * 15 = 315。不支持其他盒子的高度。
为了解决这个问题,我们将遵循以下步骤 −
定义具有长度、宽度和高度的 Box 数据类型
定义一个函数 volume(),这将采用 box,
返回 box.length * box.width * box.height
定义一个函数 lower(),它将接受 box,
如果 box.height < 41,则返回 true,否则返回 false
从主方法中,执行以下操作:,
对于初始化 i := 0,当 i < N 时,更新(将 i 增加 1),执行以下操作:
如果 lower(boxes[i]) 为 true,则:
display volume(boxes[i])
示例
让我们看看下面的实现以便更好地理解 −
#include <stdio.h> #define N 4 struct Box{ int length, width, height; }; int volume(struct Box box){ return box.length*box.width*box.height; } int lower(struct Box box){ return box.height < 41; } int solve(struct Box boxes[]){ for (int i = 0; i < N; i++) if (lower(boxes[i])) printf("%d
", volume(boxes[i])); } int main(){ struct Box boxes[N] = {{9,5,20},{3,7,15},{8,15,41},{6,3,42}}; solve(boxes); }
输入
4, {{9,5,20},{3,7,15},{8,15,41},{6,3,42}}
输出
900 315