首页 > 试题广场 >

独立的小易

[编程题]独立的小易
  • 热度指数:792 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
小易为了向他的父母表现他已经长大独立了,他决定搬出去自己居住一段时间。一个人生活增加了许多花费: 小易每天必须吃一个水果并且需要每天支付x元的房屋租金。当前小易手中已经有f个水果和d元钱,小易也能去商店购买一些水果,商店每个水果售卖p元。小易为了表现他独立生活的能力,希望能独立生活的时间越长越好,小易希望你来帮他计算一下他最多能独立生活多少天。

输入描述:
输入包括一行,四个整数x, f, d, p(1 ≤ x,f,d,p ≤ 2 * 10^9),以空格分割


输出描述:
输出一个整数, 表示小易最多能独立生活多少天。
示例1

输入

3 5 100 10

输出

11

模拟

房租是每天都要给的,水果还有剩余的话就不用买水果。如果水果吃完了,每天就多了一项买水果的开销,哪天不够付“房租”或“房租+水果”或“水果”了,就生存不下去了。
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] params = br.readLine().split(" ");
        int x = Integer.parseInt(params[0]);
        int f = Integer.parseInt(params[1]);
        int d = Integer.parseInt(params[2]);
        int p = Integer.parseInt(params[3]);
        int day = 0;
        while(d > 0){
            if(f == 0){    // 没水果了
                if(d >= p){
                    d -= p;     // 还有钱买水果
                }else{
                    break;      // 没钱买水果了
                }
            }else{
                f--;
            }
            if(d >= x){
                d -= x;      // 房租是每天都要给的
                day++;
            }else{
                break;
            }
        }
        System.out.println(day);
    }
}

发表于 2022-02-25 16:26:29 回复(0)
x, f, d, p = map(int, input().split())
if d < f*x:
    print(d//x)
elif d>=f*x:
    print((d-f*x) // (p+x) + f)
else:
    print(0)


发表于 2019-08-26 16:49:57 回复(0)
先看拥有的水果数是否大于我们能够租的天数
如果大于,则说明租完这么多天就结束了,水果还有剩,但是没钱租房了,
否则,天数就等于拥有的水果数,然后钱减少水果数*一天租金
之后就没有水果了,需要每天都去买,因此每天消费为租金+水果价格
#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
    int rent = 0;
    int ownMoney = 0;
    int ownFruit = 0;
    int fruitPrice = 0;
    cin>>rent>>ownFruit>>ownMoney>>fruitPrice;
    if(rent < 0 || ownFruit < 0 || ownMoney < 0 || fruitPrice < 0) 
    {
        cout<<0;
        return 0;
    }
    if(ownMoney < rent)
    {
        cout<<0;
        return 0;
    }
    if(0 == ownFruit && ownMoney < fruitPrice)
    {
        cout<<0;
        return 0;
    }
    int count = 0;
    if(ownFruit > 0)
    {
        int temp = ownMoney / rent;
        if(temp < ownFruit)
        {
            cout<<temp;
            return 0;
        }
        else
        {
            count = ownFruit;
            ownMoney -= (ownFruit*rent) ;
            ownFruit = 0;
        }
    }
    if(ownMoney / (rent+fruitPrice) > 0)
    {
        int temp = ownMoney / (rent+fruitPrice);
        count += temp;
        ownMoney -= (temp * (rent+fruitPrice));
    }
    cout<<count;
    return 0;
}


发表于 2021-09-24 11:34:39 回复(0)
importsys
  
defcal_days(x, f, d, p):
    ifd <=f *x:
        t =d //x
        returnt
    t =(d -f *x) //(p +x) +f
    returnt
     
if__name__=='__main__':
    line =sys.stdin.readline()
    x, f, d, p =[int(item) foritem inline.split()]
    print(cal_days(x, f, d, p))

编辑于 2019-08-03 13:51:12 回复(0)
#include<bits/stdc++.h>
using namespace std;
int x, f, d, p;
int longest_day(int x, int d, int f, int p){
    int i=f;
    if(d/x<=f) return d/x;
    while((i-f)*p+x*i<d)
        ++i; 
    return i-1;
}
 
int main(){
    cin>>x>>f>>d>>p;
    int res=longest_day(x,d,f,p);
    cout<< res<<endl;
    return 0;
}

发表于 2019-08-03 11:00:49 回复(0)
# x为每天支付房屋的租金
# f为已经有的水果数
# d为现有的资金
# p为每个水果的价格
def maxDay(x, f, d, p):
    lastOfMoney = d - f*x
    if lastOfMoney > 0:
        oneDayMoney = x + p
        dayCount = lastOfMoney//oneDayMoney + f
        print(dayCount)
    else:
        oneDayMoney = x 
        dayCount = d//oneDayMoney
        print(dayCount)
if __name__ == '__main__':
    nums = list(map(int, input().split()))
    x = nums[0]
    f = nums[1]
    d = nums[2]
    p = nums[3]
    maxDay(x, f, d, p)
发表于 2019-08-02 21:06:12 回复(0)
思路:如果水果天数多,那么就按房租的天数来算,如果房租的天数多,那么就先按水果天数来算,然后看下一天房租,一个水果多少钱,然后省下钱的就是用来买水果和房租的,最后计算出结果。
defsavelife(x, f, d, p):
    # f个水果和d元钱,小易也能去商店购买一些水果,商店每个水果售卖p元。
    # 吃1个水果并且需要每天支付x元的房屋租金
    # 计算最大天数
    maxday =d //x
    maxfruit =f
    ifmaxday > maxfruit:
        # 钱多了
        chazhimoney =d -maxfruit *x
        onedayfruitandlife =x +p
        moreday =chazhimoney //(onedayfruitandlife)
        returnmoreday +maxfruit
    else:
        returnmaxday
发表于 2019-07-25 11:51:47 回复(1)
importsys
try:
    whileTrue:
        line =sys.stdin.readline().strip()
        ifline =='':
            break
        lines =line.split(' ')
        nums =list(map(int, lines))
        if(nums[1] > nums[2]/nums[0]):
            print(int(nums[2]/nums[0]))
        else:
            print(nums[1] +int((nums[2] -nums[0] *nums[1]) /(nums[0] +nums[3])))
except:
    pass
发表于 2019-07-11 10:13:49 回复(0)