Advertisement
Valleri

C# Basics Exam 25 July 2014 Evening - 04. Nakovs Matching

Jul 30th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class NakovsMatching {
  5.     private static long getWeight(String a) {
  6.         long sum = 0;
  7.         for (int i = 0; i < a.length(); i++) {
  8.             sum += (int) a.charAt(i);
  9.         }
  10.         return sum;
  11.     }
  12.     public static void main(String[] args) {
  13.         Scanner input = new Scanner(System.in);
  14.        
  15.         String firstWord = input.nextLine();
  16.         String secondWord = input.nextLine();
  17.         int maxValue = input.nextInt();
  18.         boolean isDisplayed = false;
  19.        
  20.         for (int i = 1; i < firstWord.length(); i++) {
  21.             String aLeft = firstWord.substring(0, i);
  22.             String aRight = firstWord.substring(i, firstWord.length());
  23.            
  24.             for (int j = 1; j < secondWord.length(); j++) {
  25.                 String bLeft = secondWord.substring(0, j);
  26.                 String bRight = secondWord.substring(j, secondWord.length());
  27.                
  28.                 long nakovs = Math.abs((getWeight(aLeft) * getWeight(bRight)) - (getWeight(aRight) * getWeight(bLeft)));
  29.                
  30.                 if (nakovs <= maxValue) {
  31.                     System.out.printf("(%s|%s) matches (%s|%s) by %d nakovs\n", aLeft, aRight, bLeft, bRight, nakovs);
  32.                     isDisplayed = true;
  33.                 }
  34.             }
  35.         }
  36.         if (!isDisplayed) {
  37.             System.out.print("No");
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement