输入包括一行,四个整数x, f, d, p(1 ≤ x,f,d,p ≤ 2 * 10^9),以空格分割
输出一个整数, 表示小易最多能独立生活多少天。
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; }
分为两种情况:
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什么的变量太蛋疼了,起个有意义的变量会让思路清晰。
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(); } }
#思路 独立的天数取决于是否需要购买水果 需要购买: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; }
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); } }
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); } }
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); } }
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); } }
//特殊情况 水果很多 吃到没钱付房租 真可怜 0 0import 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)); } }