问题描述:
First, let's see a easy problem:
Last week, a candle store received 356 yuan for selling 20 candles. Each small candle sells for 11 yuan and each large candle sells for 28 yuan. How many large candles did the store sell?
Of course, the answer is 8.
So easy!
Here, the problem is below:
Last week, a candle store received M yuan for selling N candles. Each small candle sells for A yuan and each large candle sells for B yuan. How many large candles did the store sell?
输入:
Input 4 positive integers: M, N, A and B ( A < B ).
输出:
Output the count of large candles.
样例输入:
356 20 11 28
样例输出:
8
解题思路:
题目大意就是一家蜡烛店(话说这个时代还有专门卖蜡烛的店吗)在上周依靠卖n支蜡烛收到了m元,其中每一只小蜡烛单价为a元,每一支大蜡烛单价为b元,问这家蜡烛店卖了多少只大蜡烛。
使用i代表大蜡烛的数量,那么小蜡烛的数量就可以用n-i来代表,于是通过for循环的使用,当i*b+(n-i)*a=m时的i就是我们想要的大蜡烛数量。
实现代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,m,a,b,i;
scanf("%d%d%d%d",&m,&n,&a,&b);
for(i=0; i<=n; i++)
{
if(i*b+(n-i)*a==m)
{
printf("%d\n",i);
break;
}
}
}