回文数
package com.paic.cost.sharing.service.impl;
import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;
import java.util.Scanner;
public class TestPalindrome {
public static int MAX = 5001;
public static void main(String[] args) throws Exception {
// String text = "Confuciuss say:Madam,I'm Adam.";
int t, i, j, st, len, end, k, max;
// 位置数组
int[] pos = new int[MAX];
// 原始字符串数组
char[] s;
// 临时数组(只有大小写字母)
char[] s1 = new char[MAX];
System.out.println("请输入要输入的行数t:");
Scanner scanner = new Scanner(System.in);
t = scanner.nextInt();
while (t-- > 0) {
// 读取控制台的输入行信息
Scanner scanner1 = new Scanner(System.in);
String text = scanner1.nextLine();
if (StringUtils.isEmpty(text) || StringUtils.length(text) > 5000) {
System.out.println("输入字符串长度大于等于1小于等于5000");
break;
}
len = StringUtils.isNotEmpty(text) ? text.length() : 0;
s = text.toCharArray();
k = st = max = end = 0;
//预处理
for (i = 0; i < len; ++i) {
//判断是否是字母
if (CharUtils.isAsciiAlpha(s[i])) {
{
//存数位置
pos[k] = i;
if (s[i] < 97) {
//把大写转化为小写字母
s1[k++] = (char) ((int) s[i] + 32);
} else {
s1[k++] = s[i];
}
}
}
}
for (i = 0; i < k; ++i) {
//回文字符数位奇数的情况
for (j = 0; j <= i && i + j < k; ++j){
if (s1[i - j] != s1[i + j]) {
break;
}
if (j * 2 + 1 > max) {
max = 2 * j + 1;
st = pos[i - j];
end = pos[i + j];
}
}
//回文字符数为偶数的情况
for (j = 0; j <= i && i + j + 1 < k; ++j){
if (s1[i - j] != s1[i + j + 1]) {
break;
}
if (j * 2 + 2 > max) {
max = 2 * j + 2;
st = pos[i - j];
end = pos[i + j + 1];
}
}
}
for (i = st; i <= end; ++i) {
System.out.print(s[i]);
}
System.out.println();
}
}
}
循环打印ali
```java
package com.paic.cost.sharing.service.impl;
public class AliSynch {
public static class ThreadPrinter implements Runnable {
private String name;
private Object prev;
private Object self;
private ThreadPrinter(String name, Object prev, Object self) {
this.name = name;
this.prev = prev;
this.self = self;
}
@Override
public void run() {
// 循环打印20次
int count = 20;
while (count > 0) {
// 获取 prev 锁
synchronized (prev) {
// 获取 self 锁
synchronized (self) {
// 打印
System.out.print(name);
count--;
self.notifyAll();
}
try {
// 如果count==0, 最后一次打印操作,通过notifyAll操作释放对象锁。
if (count == 0) {
prev.notifyAll();
} else {
// 立即释放 prev锁,当前线程休眠,等待唤醒
prev.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) throws Exception {
// 锁对象a
Object a = new Object();
// 锁对象l
Object l = new Object();
// 锁对象i
Object i = new Object();
ThreadPrinter pa = new ThreadPrinter("a", i, a);
ThreadPrinter pb = new ThreadPrinter("l", a, l);
ThreadPrinter pc = new ThreadPrinter("i", l, i);
new Thread(pa).start();
// 保证初始ali的启动顺序
Thread.sleep(20);
new Thread(pb).start();
Thread.sleep(20);
new Thread(pc).start();
Thread.sleep(20);
}
}