3 条题解

  • 0
    @ 2025-3-31 14:09:35

    import java.util.Scanner;

    class Pencil { private int price; private int num;

    public Pencil() {
    }
    
    public Pencil(int price, int num) {
        this.price = price;
        this.num = num;
    }
    
    public int getPrice() {
        return price;
    }
    
    public void setPrice(int price) {
        this.price = price;
    }
    
    public int getNum() {
        return num;
    }
    
    public void setNum(int num) {
        this.num = num;
    }
    

    }

    public class Main { public static void main(String[] args) { //定义需要的铅笔数量 Scanner sc = new Scanner(System.in); int need = sc.nextInt();

        //定义分别购买三种铅笔的花费
        int []expenditure = new int[3];
    
        //定义三种铅笔的价格与数量
        Pencil [] arr = new Pencil[3];
        for (int i = 0; i < arr.length; i++) {
            int count= sc.nextInt();
            int price = sc.nextInt();
            arr[i] = new Pencil(price, count);
        }
    
        for (int i = 0; i < arr.length; i++) {
            //用temp来代替need,防止need被修改
            int temp = need;
    
            //用while循环来判断分别需要多少钱
            while (temp > 0) {
                temp = temp - arr[i].getNum();
                expenditure[i] += arr[i].getPrice();
            }
    
        }
    
        //输出最少花费
        //定义min
        int min = expenditure[0];
        for (int i = 1; i < expenditure.length; i++) {
            if (expenditure[i] < min) {
                min = expenditure[i];
            }
        }
        System.out.println(min);
    }
    

    }

    • 0
      @ 2025-2-16 10:39:24

      暴力解题法

      #include<bits/stdc++.h>
      using namespace std;
      int main(){
          int n,a1,a2,b1,b2,c1,c2,ta,tb,tc;
          cin>>n>>a1>>a2>>b1>>b2>>c1>>c2;
          if(n%a1==0)
              ta=n/a1*a2;
          else
              ta=(n/a1+1)*a2;
          if(n%b1==0)
              tb=n/b1*b2;
          else
              tb=(n/b1+1)*b2;
          if(n%c1==0)
              tc=n/c1*c2;
          else
              tc=(n/c1+1)*c2;
          int mix=ta;
          if(tb<=mix)
              mix=tb;
          if(tc<=mix)
              mix=tc;
          cout<<mix;
          return 0;
      }
      
      • 0
        @ 2024-12-4 21:14:42

        #include<bits/stdc++.h> using namespace std; long long n,a,b,d,mx=INT_MAX; int main(){ cin>>n; for(int i=1;i<=3;i++) { cin>>a>>b; d=ceil(double(n)/a)*b; mx=min(mx,d); } cout<<mx; return 0; }

        • 1

        信息

        ID
        5964
        时间
        1000ms
        内存
        125MiB
        难度
        1
        标签
        递交数
        506
        已通过
        281
        上传者