#my code is too long any suggestions

5 messages · Page 1 of 1 (latest)

severe garden
#

import java.util.Arrays;
import java.util.Scanner;

public class lol1 {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int N = input.nextInt();
    int A, B, C;
    A = input.nextInt();
    B = input.nextInt();
    C = input.nextInt();
    int bestMilk = N * A;
    int[] X = new int[N];
    int[] Y = new int[N];
    for (int i = 0; i < N; i++) {
        X[i] = input.nextInt();
        Y[i] = input.nextInt();
    }

    for (int temp = 1; temp <= 1000_000_000; temp++) {
        int totalMilk = 0;
       
        for (int i = 0; i < N; i++) {
            if (temp >= X[i] && temp <= Y[i]) {
                totalMilk += B;
            } else if (temp < X[i]) {
                totalMilk += A;
            } else {
                totalMilk += C;
            }
        }
      
        if (totalMilk > bestMilk) {
            bestMilk = totalMilk;
        }
    }
    
    System.out.println(bestMilk);
}

}

potent hingeBOT
#

This post has been reserved for your question.

Hey @severe garden! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

plain ember
#

when its gets too big you should divide your program into methods that does only one specific thing

#

for starters you can move both loops into their own methods