Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class NakovsMatching {
- private static long getWeight(String a) {
- long sum = 0;
- for (int i = 0; i < a.length(); i++) {
- sum += (int) a.charAt(i);
- }
- return sum;
- }
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- String firstWord = input.nextLine();
- String secondWord = input.nextLine();
- int maxValue = input.nextInt();
- boolean isDisplayed = false;
- for (int i = 1; i < firstWord.length(); i++) {
- String aLeft = firstWord.substring(0, i);
- String aRight = firstWord.substring(i, firstWord.length());
- for (int j = 1; j < secondWord.length(); j++) {
- String bLeft = secondWord.substring(0, j);
- String bRight = secondWord.substring(j, secondWord.length());
- long nakovs = Math.abs((getWeight(aLeft) * getWeight(bRight)) - (getWeight(aRight) * getWeight(bLeft)));
- if (nakovs <= maxValue) {
- System.out.printf("(%s|%s) matches (%s|%s) by %d nakovs\n", aLeft, aRight, bLeft, bRight, nakovs);
- isDisplayed = true;
- }
- }
- }
- if (!isDisplayed) {
- System.out.print("No");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement