Venciity

[SoftUni Java] ExamPreparation 04.HayvanNumber

May 24th, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class _04_HayvanNumber {
  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.                 resultCount++;
  19.                 System.out.println(number);
  20.             }
  21.         }
  22.         if (resultCount == 0) {
  23.             System.out.println("No");
  24.         }
  25.     }
  26.     public static int calculateSum (int n) {
  27.         int sum = 0;
  28.        
  29.         int n1 = n % 10;
  30.         n = n / 10;
  31.         int n2 = n % 10;
  32.         n = n / 10;
  33.         int n3 = n % 10;
  34.        
  35.         sum = n1 + n2 + n3;
  36.         return sum;
  37.     }
  38.     public static boolean isAllowed(String n) {
  39.         boolean isAllowed = true;
  40.         char[] numbers = n.toCharArray();
  41.        
  42.         for (char c : numbers) {
  43.             if (c != '5' && c != '6' && c != '7' && c != '8' && c!= '9') {
  44.                 isAllowed = false;
  45.                 return isAllowed;
  46.             }
  47.         }
  48.         return isAllowed;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment