Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- for (int i=1; i<=5; i++) {
- System.out.println("Enter input:");
- Scanner input = new Scanner(System.in);
- String value = input.nextLine();
- System.out.println("Enter target:");
- int target = input.nextInt();
- input.close();
- while (value.length() != 4)
- value = ("0" + value);
- int a = value.charAt(0)-'0';
- int b = value.charAt(1)-'0';
- int c = value.charAt(2)-'0';
- int d = value.charAt(3)-'0';
- int combo1 = a + b + c + d;
- int combo2 = (a*1000) + (b*100) + (c*10) + d;
- int combo3 = a + (b*100) + (c*10) + d;
- int combo4 = ((a*10) + b) + ((c*10) + d);
- int combo5 = ((a*100) + (b*10) + c + d);
- int combo6 = a + (b*10 + c) + d;
- int combo7 = ((a*10) + b) + c + d;
- int combo8 = a + b + ((c*10)) + d;
- int best = 0;
- if (combo1 < target) {
- best = combo1;
- }
- if (combo2 < target && combo2 > best) {
- best = combo2;
- }
- if (combo3 < target && combo3 > best) {
- best = combo3;
- }
- if (combo4 < target && combo4 > best) {
- best = combo4;
- }
- if (combo5 < target && combo5 > best) {
- best = combo5;
- }
- if (combo6 < target && combo6 > best) {
- best = combo6;
- }
- if (combo7 < target && combo7 > best) {
- best = combo7;
- }
- if (combo8 < target && combo8 > best) {
- best = combo8;
- }
- System.out.println(best);
- }
- }
Add Comment
Please, Sign In to add comment