HNUST-OJ-1689-So easy

问题描述:

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: MNand 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;
        }
    }






}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值