输入包括一行,四个整数x, f, d, p(1 ≤ x,f,d,p ≤ 2 * 10^9),以空格分割
输出一个整数, 表示小易最多能独立生活多少天。
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); } }
#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; }