C语言-计算一颗树的高度

C语言练习。

在这里插入图片描述
此处的单位为英寸(inches),一英尺(feet)=12寸。

	long shorty = 0L;
	long lofty = 0L;
	long feet = 0L;
	long inches = 0L;
	long shorty_to_lofty = 0L;
	long lofty_to_tree = 0L;
	long tree_height = 0L;
	const long inches_per_foot = 12L;

	printf("Enter lofty's height to the top of his/her head, in whole feet: ");
	scanf("%ld", &feet);
	printf("			...and then inches: ");
	scanf("%ld", &inches);
	lofty = feet * inches_per_foot + inches;

	printf("Enter Shorty's height up to his/her eyes, in whole feet: ");
	scanf("%ld", &feet);
	printf("							...and then inches: ");
	scanf("%ld", &inches);
	shorty = feet * inches_per_foot + inches;

	// Get the distance from Shorty to Lofty
	printf("Enter the distance between Shorty and Lofty, in whole feet: ");
	scanf("%ld", &feet);
	printf("								...and the inches: ");
	scanf("%ld", &inches);
	shorty_to_lofty = feet * inches_per_foot + inches;

	// Get the distance from Lofty to the tree
	printf("Finally enter the distance from Lofty to the tree to the nearest foot: ");
	scanf("%ld", &feet);
	lofty_to_tree = feet * inches_per_foot;

	// Calculate the height of the tree in inches
	tree_height = shorty + (shorty_to_lofty + lofty_to_tree) *
			(lofty - shorty) / shorty_to_lofty;

	printf("The height of the tre is %ld feet and %ld inches.\n",
			tree_height/inches_per_foot, tree_height % inches_per_foot);

输入和输出结果:

Enter lofty's height to the top of his/her head, in whole feet: 6
			...and then inches: 2
Enter Shorty's height up to his/her eyes, in whole feet: 4
							...and then inches: 6
Enter the distance between Shorty and Lofty, in whole feet: 5
								...and the inches: 0
Finally enter the distance from Lofty to the tree to the nearest foot: 20
The height of the tre is 12 feet and 10 inches.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值