首页 > 试题广场 >

独立的小易

[编程题]独立的小易
  • 热度指数: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
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)
import java.util.Scanner;

public class Main{     public static Scanner scan=new Scanner(System.in);          public static void main(String[] args) {                  int x=scan.nextInt();         int f=scan.nextInt();         int d=scan.nextInt();         int p=scan.nextInt();         //在水果吃完时交的房费         int Beformoney = f*x;
        //在水果吃完,交完的房费,剩下的钱         int aftermoney=d-Beformoney;         // 如果还有剩余的钱         if(aftermoney>0) {
             //得出交完房费和水果钱的生活天数             int num = aftermoney/(x+p);             if(num>=0) {                 System.out.println(num+f);             }else {
                //剩下的钱不够一天的生活                 System.out.println(f);             }
            //钱透支了,在f个水果没吃完,钱已经不够交房租了
            //求交了几天房费         }else if(aftermoney<0) {             System.out.println(d/x);         }else {
            //正好吃完水果,钱也够交房租             System.out.println(f);         }     }
}

发表于 2019-06-28 13:27:06 回复(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)

一开始使用的模拟,时间复杂度有点高:

private static int solution(int x, int f, int d, int p) {
    int res = 0;

    if (x > d) return res;

    while (f > 0 && d > 0) {
        d -= x;
        f--;
        res++;
    }

    if (d <= 0) return res;

    while (d > p + x) {
        d -= (p + x);
        res++;
    }

    return res;
}

仔细考虑下,只有三种情况:

private static int solution(int x, int f, int d, int p) {
    if (x > d) return 0;  // 带的钱交不起房租

    if (d - (x * f) > 0) {  // 在苹果吃完之后还有剩余的钱
        int m = d - (x * f);
        return f + m / (p + x);
    } else {  // 苹果没吃完之前就没钱了
        return d / x;
    }
}
发表于 2019-05-17 09:24:50 回复(0)
import java.io.*;
public class Main {
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[]args)throws Exception {
        String str=br.readLine();
        int x=0,f=0,d=0,p=0;
        if(str!=null) {
            String []strArray=str.trim().split(" ");
            x=Integer.parseInt(strArray[0]);
            f=Integer.parseInt(strArray[1]);
            d=Integer.parseInt(strArray[2]);
            p=Integer.parseInt(strArray[3]);
            int days=f;
            if(d/x<=f) {
                days=d/x;
            }else {
                d=d-days*x;
                days=f+d/(x+p);
            }
            System.out.println(days);
        }
        
    }
}
 
发表于 2019-04-19 16:26:05 回复(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();
        int d=sc.nextInt();
        int p=sc.nextInt();
        int t=0;
        if((d/x)>=f){
            t=(d-f*x)/(p+x)+f;//分子代表余下的钱,分母代表一天的开支(1个水果加房租)
           //
            System.out.println(t);
        }else{
            System.out.println(d/x);//钱少,水果多的情况
        }
            
    }
}
发表于 2019-04-12 20:52:33 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int x = sc.nextInt();    //房租
            int f = sc.nextInt();    //已有水果
            int d = sc.nextInt();    //钱
            int p = sc.nextInt();    //水果售价
            int days1 = AliveDays(x,f,d,p);//Method 1
            long days2 = AliveDays2(x,f,d,p);//Method 2
            System.out.println(days1);
            System.out.println(days2);
        }
    }
    public static int AliveDays(int x,int f,int d,int p) {
        int count = d;
        int days = 0;
        while(count > 0) {
            if(f != 0) {
                if(count - x >= 0) {
                    days++;
                    f--;
                    count = count - x;
                }
                else {
                    break;
                }
            }
            else if(f == 0){
                if(count - x - p >= 0) {
                    days++;
                    count = count - x - p;
                }
                else {
                    break;
                }
            }
        }
        return days;
    }
    public static long AliveDays2(long x,long f,long d,long p) {
        if(d/x <= f) {        //如果苹果足够多,只需用钱来租房子
            return d/x;
        }
        else {                //如果苹果不多,把已有的苹果换算成钱,加上已有的钱用来租房子和买苹果
            return (d + f*p)/(x + p);
        }    
    }
}

编辑于 2019-04-06 14:48:00 回复(0)
import java.util.Scanner;
public class Main {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         long house = sc.nextInt();         long firut = sc.nextInt();         long money  = sc.nextInt();
        long cost = sc.nextInt();
        long nowMoney = firut*cost+money;         long oneday = house + cost;         if (nowMoney/oneday >= money/house){             System.out.println((int)Math.floor(money/house));         }         else {             System.out.println((int)Math.floor(nowMoney/oneday));         }     } }
假设把水果卖了,反正最后都是当钱花,需要注意的一点是,int类型有的测试案例会超出int的范围。
还有在假设把水果卖掉的时候需要考虑如果最后一天水果有没有吃完
发表于 2019-04-03 15:34:31 回复(0)
保证每天都有第二天的租金和水果吃,要注意初始就没有水果吃、没有租金的情况
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String[] s=sc.nextLine().split(" ");
        int x=Integer.parseInt(s[0]);
        int f=Integer.parseInt(s[1]);
        int d=Integer.parseInt(s[2]);
        int p=Integer.parseInt(s[3]);
        
        int day=0;
        if(f==0 && d<(x+p)) {
            System.out.println(0);
        }
        if(f==0 && d>=(x+p)) {
            f=1;
            d-=p;
        }
        
        while(d>=x && f>=1) {
            day++;
            d-=x;
            f--;
            if(f==0) {
                d-=p;
                f=1;
            }
        }
        System.out.println(day);
        
        
    }

}

发表于 2019-03-25 21:48:55 回复(0)
//为什么要报。请检查是否存在数组越界等非法访问情况,case通过率为50.00%
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
            long x = sc.nextLong();
            long f = sc.nextLong();
            long d = sc.nextLong();
            long p = sc.nextLong();
            getResult(x, f, d, p, 0);
    }

    public static void getResult(long x, long f, long d, long p, long count) {
        if (f > 0 && (d - x) >= 0) {
            getResult(x, f - 1, d - x, p, count + 1);
        } else if (f == 0 && (d - x - p) >= 0) {
            getResult(x, f, d - x - p, p, count + 1);
        } else {
            System.out.println(count);
            return;
        }
    }
}

发表于 2019-03-09 20:35:51 回复(1)
        //思路:
        //1.先求出当前的钱能租多少天房子,即租房天数;
        //2.然后与苹果数比较;
        //3.如果租房天数小于等于苹果数,那么只能住的时间就是租房的天数;
        //4.否则,苹果数少,那么,就可以去购买苹果。
        //5.生存的天数=当前苹果数+吃完当前苹果还可以生存多少天;
        //6.后者=剩余的钱数/(苹果单价+房租)
        //7.剩余的钱数=当前的钱数-花去的钱数;
        //8.花去的钱数=苹果数*房租金(因为一天一个苹果)
发表于 2019-01-29 16:55:26 回复(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 java.util.Scanner;

public class text5 {
public static void main(String args[]){
int[] arr=new int[4];
Scanner sc= new Scanner(System.in);
for(int i=0;i<4;i++){
arr[i]=sc.nextInt();
}
int temp=arr[2]-arr[0]*arr[1];
if(temp<0){
System.out.println((int)(arr[2]/arr[0]));
}else{
System.out.println(arr[1]+(int)(temp/(arr[0]+arr[3])));
}
}

}

发表于 2017-09-09 16:54:03 回复(0)
importjava.util.Scanner;
publicclassMain{
    publicstaticvoidmain(String [] args){
        Scanner scanner=newScanner(System.in);
        longx=scanner.nextLong();
        longf=scanner.nextLong();
        longd=scanner.nextLong();
        longp=scanner.nextLong();
        scanner.close();
         
        longday=0;
        //1.水果在钱花完之前够吃,就不用买水果,考虑房租的钱;2.钱用来支付房租和水果
        if(d/x<=f){
            day=d/x;
            System.out.print(day);
        }
        else{
            longmoney=0;
            money=d+p*f;
            day=money/(x+p);
            System.out.print(day);
        }
    }
}
发表于 2017-08-13 11:39:09 回复(0)
package test2;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long x = scanner.nextLong();
long f = scanner.nextLong();
long d = scanner.nextLong();
long p = scanner.nextLong();
if(d/x>f){
System.out.println((d+f*p)/(x+p));

}else if((d/x)==f){
System.out.println(f);
}
else {
System.out.println(d/x);
}
}

}
编辑于 2017-08-12 18:47:51 回复(0)