首页 > 试题广场 >

独立的小易

[编程题]独立的小易
  • 热度指数:27137 时间限制: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

如果开始持有的水果够吃 直接算d/x就是天数
如果不够吃 则可以等效为一开始没有水果 而是多了p*f块钱
由于一开始不知道水果够不够吃, 所以比较这两种那个天数少就是哪个。
ps:这种应该是最简单的编程方法
注意:题目给到了2*
10^9的数量级 也就是int型的最大数量级 如果再做乘法加法运算就会爆了 所以用long long型(此题的测试样例很温和,没有给大数)

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    long long x, f, d, p; cin>>x>>f>>d>>p;
    int ans = min((d + p*f) / (x + p), d/x);
    printf("%d\n", ans);
    return 0;
}
编辑于 2019-03-01 21:38:45 回复(0)

python两行

分为两种情况:

  1. 带水果够用。
  2. 带的水果不够用,还需要继续买水果。
rent_price, fruit_num, money, fruit_price = map(int, input().split())
print(money // rent_price if money // rent_price < fruit_num else fruit_num + (money - rent_price * fruit_num) // (fruit_price + rent_price))

题目输入的x,d,p,f什么的变量太蛋疼了,起个有意义的变量会让思路清晰。

发表于 2019-03-02 11:16:49 回复(0)
以下代码,不考虑水果可以出售的情况
如果水果可以出售就更简单了,把水果换算成钱,除以每天的花费(房租和水果)
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int rentPay=sc.nextInt();
		int appleNum=sc.nextInt();
		int totalMoney=sc.nextInt();
		int applePrice=sc.nextInt();
		
		if(totalMoney/rentPay<=appleNum)
			System.out.println(totalMoney/rentPay);
		else
			System.out.println(appleNum+(totalMoney-appleNum*rentPay)/(applePrice+rentPay));
		
		sc.close();
	}
} 


编辑于 2017-08-16 12:23:54 回复(1)
#思路
独立的天数取决于是否需要购买水果
需要购买:1.总的金额 d 减去 已有水果个数与房租的乘积 (x*f),即在不需要购买水果的天数里消耗的金额
         2.比较剩余金额与房租加水果的大小,若大于则可以继续独立生活 days=mid(p+x)
                                         若小于则结束独立生活
不需要购买:即在金额在被房租消耗完后,水果刚好够或者还有剩余
           此时,独立的天数为 金额/房租 即只需付房费

其实,分析了问题挺简单的
#include <iostream>

using namespace std;

int main()
{
    int x,f,d,p;
    int mid;
    cin>>x>>f>>d>>p;
    int days=0;
    if(x*f<d)
    {
        mid=d-x*f;
        if(mid>p+x)
        {
            days=mid/(p+x);
            cout<<days+f;
        }
        else
        {
            cout<<f;
        }
    }
    if(x*f>=d)
    {
        cout<<(d/x);
    }
    return 0;
}

发表于 2019-05-13 18:41:17 回复(0)
顺序逻辑下来就好
importjava.util.ArrayList;
importjava.util.Scanner;
publicclassMain {
    publicstaticvoidmain(String[] args) {
        Scanner sc =newScanner(System.in);
        while(sc.hasNextInt()) {
            intx = sc.nextInt();
            intf = sc.nextInt();
            intd = sc.nextInt();
            intp = sc.nextInt();
            intsumday =0;
            while(d >=0) {
                if(f >0) {
                    f--;
                }else{
                    d -= p;
                }
                d -= x;
                sumday++;
            }
            System.out.println(sumday -1);
        }
    }
}
发表于 2017-08-22 15:58:55 回复(0)
分为两种情况
1.手里的钱不够租房f天,此时只能独立生活d/x天;
2.手里的钱可以租房超过f天,此时支付完f天的房租后,每天又增加了购买水果的p元,还能独立生活(d-f*x)/(x+p)天。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] strArr = br.readLine().trim().split(" ");
        int x = Integer.parseInt(strArr[0]);
        int f = Integer.parseInt(strArr[1]);
        int d = Integer.parseInt(strArr[2]);
        int p = Integer.parseInt(strArr[3]);
        int days;
        if(d/x > f){
            // 如果能租房的天数d/x大于手上已有的水果数f,则需要继续每天购买水果和付房租
            days = f + (d - f*x)/(x + p);
        }else{
            // 否则只能独立d/x天
            days = d/x;
        }
        System.out.println(days);
    }
}


编辑于 2021-02-15 22:07:47 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main(){
    int x, f, d, p;
    scanf("%d%d%d%d", &x, &f, &d, &p);
    if(x*f>=d)
        printf("%d\n", int(d/x));
    else
        printf("%d\n", f + (d-x*f)/(x+p));
    return 0;
}

发表于 2020-09-25 00:55:55 回复(0)
Java语言     
import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin=new Scanner (System.in);
		long x=cin.nextInt();//一天的租金
		long f=cin.nextInt();//手中已有的水果数
		long d=cin.nextInt();//总的现金
		long p=cin.nextInt();//一个水果的价格
		long out=0;
		if(d/x>=f) {
			
			out=out+((d-f*x)/(x+p))+f;
		}
		else {
			out=d/x;
		}
		/*
		 * for (int i =0;;i++) {
			if(d<x)break;
			else if(d>=x && (f<=0&&d<x+p))break;
			else {
				d=d-x;
				if(f>0)f--;
				else if(f<=0){
					d=d-p;
				}
				out++;
			}
		}
		 */
		System.out.print(out);

	}

}

看代码说话  
发表于 2019-11-05 14:58:14 回复(0)

1.有水果就每天就交租房费,水果量减1。
2.没水果就同时交租房费和水果费。

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int f = sc.nextInt();
        int d = sc.nextInt();
        int p = sc.nextInt();
        int total = 0;

        while (d >= 0) {
            if (f > 0) {
                d -= x;
                f--;
            } else {
                d -= (x + p);
            }
            if (d >= 0) {
                total++;
            }
        }


        System.out.println(total);
    }
}
发表于 2019-06-10 15:07:04 回复(0)
#include <iostream>
using namespace std;
int main(){
    
    long long x,f,d,p;
    cin >> x >> f >> d >> p;
    
    cout << min((d + (f * p)) / (x + p), (d / x)) << endl;
    return 0;
}

发表于 2019-06-04 20:59:04 回复(0)
#include<iostream>
using namespace std;
int main(){
    long x, f, d, p;
    cin >> x >> f >> d >> p;
    if ((d / x) > f){
        cout << (d + f*p) / (x+p) << endl;
    }
    else{
        cout << d / x << endl;
    }
    return 0;
}

发表于 2019-04-19 21:04:04 回复(0)
import java.util.*;
public class Main{
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();//租金
        int f=sc.nextInt();//f个水果
        int d=sc.nextInt();//d元钱
        int p=sc.nextInt();//水果单价
        int day=0;
        if(x*f>=d) day=d/x;
        else{
            day=f+(d-x*f)/(x+p);
        }
        System.out.println(day);
    }
}
发表于 2019-04-06 11:49:38 回复(0)
数学题
#include<bits/stdc++.h>
using namespace std;
int main() {
    int x, f, d, p;
    cin >> x >> f >> d >> p;
    cout << ((x * f < d) ? (f + (d - x * f) / (x + p)) : (d / x)) << endl;
    return 0;
}

发表于 2019-03-16 22:18:44 回复(0)
def independent(x, f, d, p):
    if d/x <= f:  # 当支持不到f天时,只需要付房钱,不用买水果
        return d//x
    else:  # 当超过f天,需要服f天房钱,和后续每天x+p的房钱和水果钱
        return f+(d-f*x)//(x+p)  
    
if __name__ == "__main__":
    x, f, d, p = map(int, input().split())
    print(independent(x, f, d, p))

发表于 2019-03-14 11:28:04 回复(0)
 
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
    long x, f, d, p, day;
    cin >> x >> f >> d >> p;
    day = (d < x*f)? day = d/x : day = (d-x*f)/(p+x) + f;   
    cout << day << endl;
}
太简单了吧。。。
发表于 2019-02-18 15:31:20 回复(0)
import java.util.Scanner;

public class wy5 {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int f = sc.nextInt();
        int d = sc.nextInt();
        int p = sc.nextInt();
        System.out.print(liveDays(x, f, d, p));
    }

    /**
     * 思路:如果水果足够多,只需要算能支付多少天房租就可以。否则计算f+(d-f*x)/(x+p)
     * @param x:每天房租
     * @param f:已有f个水果
     * @param d:已有d元钱
     * @param p:商店每个水果卖p元
     * @return */
    public static int liveDays(int x, int f, int d, int p) {
        if (x * f >= d)
            return d / x;
        d = d - f * x;
        return f + d / (x + p);
    }
}

编辑于 2019-01-17 13:12:31 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        try(Scanner in = new Scanner(System.in)){
            int x = in.nextInt(),f = in.nextInt(),d = in.nextInt(),p = in.nextInt();
            System.out.println(helper(x,f,d,p));
        }
    }
    public static int helper(int x,int f,int d,int p){
        if(d / x <= f) return d / x;
        d -= x * f;
        return f + d / (x + p);
    }
}

发表于 2019-01-12 15:09:03 回复(0)
import sys
x,f,d,p=list(map(int,sys.stdin.readline().strip().split()))
day=(d+f*p)//(p+x)
print(min(day,d//x))
发表于 2018-05-08 16:34:19 回复(0)
//特殊情况 水果很多 吃到没钱付房租 真可怜 0 0
import java.util.Scanner;  public class Main {     public static long cal(long x, long f, long d, long p) {         if(f>d/x)    return d/x;         return (d - x*f)/(p+x) +f;     }     public static void main(String[] args) {                  Scanner s = new Scanner(System.in);         long x = s.nextLong();         long f = s.nextLong();         long d = s.nextLong();         long p = s.nextLong();         System.out.println(cal(x, f, d, p));             } }
发表于 2018-03-24 20:24:59 回复(0)
#include<stdio.h>
int main(){
    long long x, f, d, p;
    while (scanf("%lld%lld%lld%lld", &x, &f, &d, &p) != EOF){
        if (d / x <= f) printf("%lld\n", d / x);
        else{
            printf("%lld\n", (f*p + d) / (x + p));
        }
    }
}

发表于 2018-02-28 10:04:32 回复(0)