百度3.13后端笔试第二道
r,e,d三个字符,能否构成含有 cnt 个回文串的字符串 s
原理:n个相同的字符构建的回文子串的个数为n(n+1)/2,其余的用edr补
```
public static void deal1() {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int n = (int) (Math.sqrt(2*x + 0.25) - 0.5);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append("r");
}
n = n * (n+1) / 2;
int y = (x - n) / 3;
for (int i = 0; i < y; i++) {
sb.append("edr");
}
if (n + y * 3 == x) {
System.out.println(sb);
} else if (n + y * 3 + 1 == x) {
sb.append("e");
System.out.println(sb);
} else if (n + y * 3 + 2 == x) {
sb.append("ed");
System.out.println(sb);
}
```
原理:n个相同的字符构建的回文子串的个数为n(n+1)/2,其余的用edr补
```
public static void deal1() {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int n = (int) (Math.sqrt(2*x + 0.25) - 0.5);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append("r");
}
n = n * (n+1) / 2;
int y = (x - n) / 3;
for (int i = 0; i < y; i++) {
sb.append("edr");
}
if (n + y * 3 == x) {
System.out.println(sb);
} else if (n + y * 3 + 1 == x) {
sb.append("e");
System.out.println(sb);
} else if (n + y * 3 + 2 == x) {
sb.append("ed");
System.out.println(sb);
}
```
全部评论
相关推荐
躺平了的秋田犬很想吃烤肉:光北京户口就值多少钱呢
点赞 评论 收藏
分享