Advertisement
milen_vm

Variant5 MagicDates

May 19th, 2014
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. import java.time.LocalDate;
  2. import java.time.Month;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class MagicDates {
  7.  
  8.     public static void main(String[] args) {       
  9.         Scanner input = new Scanner(System.in);
  10.         int startYear = input.nextInt();
  11.         int endYear = input.nextInt();
  12.         int magicWeight = input.nextInt();
  13.         LocalDate startDate = LocalDate.of(startYear, Month.JANUARY, 1);
  14.         LocalDate endDate = LocalDate.of(endYear, Month.DECEMBER, 31);
  15.         boolean noMagicDates = true;
  16.         LocalDate currentDate = startDate;
  17.        
  18.         while (currentDate.isBefore(endDate) || currentDate.isEqual(endDate)) {
  19.             int d1 = currentDate.getDayOfMonth() / 10;     
  20.             int d2 = currentDate.getDayOfMonth() % 10;
  21.                 int d3 = currentDate.getMonthValue() / 10;
  22.                 int d4 = currentDate.getMonthValue() % 10;
  23.                 int d5 = (currentDate.getYear() / 1000) % 10;
  24.                 int d6 = (currentDate.getYear() / 100) % 10;
  25.                 int d7 = (currentDate.getYear() / 10) % 10;
  26.                 int d8 = (currentDate.getYear() / 1) % 10;
  27.                 int[] digits = { d1, d2, d3, d4, d5, d6, d7, d8 };
  28.                 int weight = 0;
  29.            
  30.                 for (int i = 0; i < digits.length; i++){
  31.        
  32.                     for (int j = (i + 1); j < digits.length; j++){
  33.                             weight += digits[i] * digits[j];
  34.                     }
  35.                 }
  36.                 if (weight == magicWeight){
  37.                     System.out.printf("%1$td-%1$tm-%1tY%n", currentDate);
  38.                     noMagicDates = false;
  39.                 }
  40.    
  41.                 currentDate = currentDate.plusDays(1);
  42.          }
  43.          if (noMagicDates){
  44.              System.out.println("No");
  45.          }     
  46.     }  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement