栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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.
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/965881.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号