1000个数,数值的范围是[0,999]有且只有两个相同的数请编写java程序找出来
import java.util.HashMap;
import java.util.Map;
public class FindDuplicates {
public static void main(String[] args) {
int[] numbers = {/* 在这里填入你的1000个数 */};
findDuplicates(numbers);
}
public static void findDuplicates(int[] numbers) {
Map<Integer, Integer> countMap = new HashMap<>();
for (int num : numbers) {
countMap.put(num, countMap.getOrDefault(num, 0) + 1);
if (countMap.get(num) == 2) {
System.out.println("The duplicate numbers are: " + num);
return; // 找到后就返回,因为题目说有且仅有两个相同的数
}
}
System.out.println("No duplicate numbers found or more than two duplicates found.");
}
}
不使用Java的split将字符串拆分成数组,如ab&&2通过&&拆分成[ab,2]
public class Main {
public static void main(String[] args) {
String str = "ab&&2";
String delimiter = "&&";
int index = str.indexOf(delimiter);
if (index != -1) {
String[] result = new String[2];
result[0] = str.substring(0, index);
result[1] = str.substring(index + delimiter.length());
// 输出结果
for (String s : result) {
System.out.println(s);
}
} else {
System.out.println("Delimiter not found in the string.");
}
}
}
不使用Java的组合函数将字符串ab,2通过&&组合成ab&&2
public class Main {
public static void main(String[] args) {
String firstPart = "ab";
String secondPart = "2";
String delimiter = "&&";
// 创建结果字符串,手动拼接
StringBuilder result = new StringBuilder();
result.append(firstPart);
result.append(delimiter);
result.append(secondPart);
// 输出最终结果
System.out.println(result.toString());
}
}
有一张数据库表其表结构为:
create table student(
id int,
name varchar(32),
age int,
score double(4, 1),
birthday date,
sex int,
insert_time timestmp
);
请按照年龄(age)分组 查出不同组的平均分和人数,分数score低于60分的人不参与分组某一年龄分组人数小于 2,该年龄段不参与分组,请按照以上要求写出sql 语句
SELECT age,
AVG(score) as average_score,
COUNT(*) as number_of_students
FROM (
SELECT id, age, score
FROM student
WHERE score >= 60 -- 排除分数低于60分的人
) AS filtered_students
GROUP BY age
HAVING COUNT(*) >= 2 -- 忽略人数小于2的年龄组
ORDER BY age;
双线程交替打印0-100的三种方法
方法一:传统wait/notifyAll+等待标记mark
static AtomicInteger i = new AtomicInteger(1);
static Object lock = new Object();
static int mark = 1;
Thread a = new Thread(() -> {
while (i.get() < 100){
synchronized (lock){
while (mark != 1){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程a:" + i.getAndIncrement());
lock.notifyAll();
mark = 2;
}
}
});
Thread b = new Thread(() -> {
while (i.get() <= 100){
synchronized (lock){
while (mark != 2){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程b:"+i.getAndIncrement());
lock.notifyAll();
mark = 1;
}
}
});
a.start();
b.start();
方法二:信号量Semaphore
static AtomicInteger i = new AtomicInteger(1);
static Semaphore s1 = new Semaphore(1);
static Semaphore s2 = new Semaphore(0);
Thread a = new Thread(() -> {
try {
while(i.get() < 100){
s1.acquire();
System.out.println("线程-a:" + i.getAndIncrement());
s2.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread b = new Thread(() -> {
try {
while(i.get() < 100){
s2.acquire();
System.out.println("线程-b:" + i.getAndIncrement());
s1.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
a.start();
b.start();
方法三:LockSupport
static AtomicInteger i = new AtomicInteger(1);
Thread [] threads = new Thread[2];
threads[0] = new Thread(() -> {
while (i.get() < 100){
System.out.println("线程a:" + i.getAndIncrement());
LockSupport.unpark(threads[1]);
LockSupport.park();
}
});
threads[1] = new Thread(() -> {
while (i.get() < 100){
LockSupport.park();
System.out.println("线程b:" + i.getAndIncrement());
LockSupport.unpark(threads[0]);
}
});
threads[0].start();
threads[1].start();