Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

阳阳买苹果——c语言算法实现

$
0
0

题目:


 阳阳买苹果,每个苹果0.8元,第一天他买2个,第二天开始每天买前一天的2倍,直到购买的苹果个数不超过100个的最大值,编程求阳阳每天平均花多少钱?


解析:


这个算法和“猴子吃桃”有些类似,只不过求桃子是倒着求,苹果是正着求,只要用一个循环就得了,条件就是苹果个数不超过100,贴上源码:


#include "stdio.h"

void main()

{
	int day = 0;
	float money = 0;
	int a;
	
	for (a = 2;a < 100;day++,a *= 2)
	{
		money = a * 0.8 + money;
	}
	printf("阳阳一共买了%d天苹果,他平均每天花%f元钱。\n",day,money / day);
}






作者:byhuamo 发表于2013-11-21 23:08:34 原文链接
阅读:46 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>