Created
March 26, 2020 18:03
-
-
Save belushkin/51e6d7a7ce51b05237724f628df0054b to your computer and use it in GitHub Desktop.
K-th Beautiful String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class cf { | |
public static void main(String[] args) { | |
// Use the Scanner class | |
Scanner sc = new Scanner(System.in); | |
int n = Integer.parseInt(sc.nextLine()); | |
for (int i = 0; i < n; i++) { | |
String s = sc.nextLine(); | |
String[] result = s.split(" "); | |
int a = Integer.parseInt(result[0]); | |
int b = Integer.parseInt(result[1]); | |
String str = "a"; | |
String repeated = String.join("", Collections.nCopies(a, str)); | |
StringBuilder sb = new StringBuilder(repeated); | |
if (b == 1) { | |
sb.setCharAt(repeated.length()-1,'b'); | |
sb.setCharAt(repeated.length()-2,'b'); | |
System.out.println(sb.toString()); | |
} else { | |
int sum = 1; | |
for (int j = 2; j < a; j++) { | |
sum = sum + j; | |
if (b <= (sum)) { | |
sb.setCharAt(repeated.length()-(j+1),'b'); | |
sb.setCharAt(repeated.length()-(j-(sum-b)),'b'); | |
// System.out.println("sum:" + sum + ", j: " + j + ", b:" + b); | |
System.out.println(sb.toString()); | |
break; | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment