Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class _4_MagicStrings {
- private static char[] letters = { 'k', 'n', 'p', 's' };
- private static int[] weightOfLetters = { 1, 4, 5, 3 };
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- int diff = scan.nextInt();
- boolean foundMagicString = false;
- if (diff <= 16) {
- foundMagicString = findMagicString(diff);
- }
- if (!foundMagicString) {
- System.out.println("No");
- }
- }
- private static boolean findMagicString(int diff) {
- // TODO Auto-generated method stub
- boolean isFound = false;
- int[] index = new int[8];
- int firstWeight, secondWeght, diffBetween;
- for (index[0] = 0; index[0] < letters.length; index[0]++) {
- for (index[1] = 0; index[1] < letters.length; index[1]++) {
- for (index[2] = 0; index[2] < letters.length; index[2]++) {
- for (index[3] = 0; index[3] < letters.length; index[3]++) {
- firstWeight = weightOfLetters[index[0]]
- + weightOfLetters[index[1]]
- + weightOfLetters[index[2]]
- + weightOfLetters[index[3]];
- for (index[4] = 0; index[4] < letters.length; index[4]++) {
- for (index[5] = 0; index[5] < letters.length; index[5]++) {
- for (index[6] = 0; index[6] < letters.length; index[6]++) {
- for (index[7] = 0; index[7] < letters.length; index[7]++) {
- secondWeght = weightOfLetters[index[4]]
- + weightOfLetters[index[5]]
- + weightOfLetters[index[6]]
- + weightOfLetters[index[7]];
- diffBetween = Math.abs(firstWeight
- - secondWeght);
- if (diffBetween == diff) {
- isFound = true;
- printTheMagicString(index);
- }
- }
- }
- }
- }
- }
- }
- }
- }
- return isFound;
- }
- private static void printTheMagicString(int[] indexArr) {
- // TODO Auto-generated method stub
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < indexArr.length; i++) {
- char letter = letters[indexArr[i]];
- sb.append(letter);
- }
- System.out.println(sb.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement