AlexKondov

Java Hayvan Numbers

May 24th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class HayvanNumbers {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);
  8.         int sum = input.nextInt();
  9.         int diff = input.nextInt();
  10.         int resultCount = 0;
  11.        
  12.         for (int n1 = 555; n1 <= 999; n1++) {
  13.             int n2 = n1 + diff;
  14.             int n3 = n2 + diff;
  15.             String number = "" + n1 + n2 + n3;
  16.            
  17.             if(isAllowed(number)&& (calculateSum(n1) + calculateSum(n2) + calculateSum(n3) == sum)) {
  18.                 System.out.printf("%d%d%d", n1, n2, n3);
  19.                 resultCount++;
  20.                 System.out.println(number);
  21.             }
  22.         }
  23.         if (resultCount == 0) {
  24.            
  25.         }
  26.     }
  27.     public static int calculateSum (int n) {
  28.         int sum = 0;
  29.        
  30.         int n1 = n % 10;
  31.         n = n / 10;
  32.         int n2 = n % 10;
  33.         n = n / 10;
  34.         int n3 = n % 10;
  35.        
  36.         sum = n1 + n2 + n3;
  37.         return sum;
  38.     }
  39.     public static boolean isAllowed(String n) {
  40.         boolean isAllowed = true;
  41.         char[] numbers = n.toCharArray();
  42.        
  43.         for (char c : numbers) {
  44.             if (c != '5' && c != '6' && c != '7' && c != '8' && c!= '9') {
  45.                 isAllowed = false;
  46.                 return isAllowed;
  47.             }
  48.         }
  49.         return isAllowed;
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment