9.4
This commit is contained in:
@ -0,0 +1,24 @@
|
||||
package com.raorao.interview.xiecheng.t1;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-04-19:40
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
long input = scanner.nextLong();
|
||||
|
||||
int count = 0;
|
||||
while (input != 0) {
|
||||
input = input & (input - 1);
|
||||
count++;
|
||||
}
|
||||
System.out.println(count);
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package com.raorao.interview.xiecheng.t2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-04-19:43
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int target = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
D[] dd = new D[n];
|
||||
List<D> ret = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
String[] temp = scanner.nextLine().split(" ");
|
||||
int id = Integer.parseInt(temp[0]);
|
||||
int start = Integer.parseInt(temp[1]);
|
||||
int end = Integer.parseInt(temp[2]);
|
||||
dd[i] = new D(id, start, end);
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (target >= dd[i].start && target <= dd[i].end) {
|
||||
ret.add(dd[i]);
|
||||
}
|
||||
}
|
||||
ret.sort(Comparator.comparingInt(e -> e.id));
|
||||
if (ret.size() == 0) {
|
||||
System.out.println("null");
|
||||
return;
|
||||
}
|
||||
ret.forEach(e -> System.out.println(e.id));
|
||||
|
||||
}
|
||||
|
||||
private static class D {
|
||||
|
||||
int id;
|
||||
int start;
|
||||
int end;
|
||||
|
||||
public D(int id, int start, int end) {
|
||||
this.id = id;
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
public D() {
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
package com.raorao.interview.xiecheng.t3;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* 输入: 2 p 1 1 p 2 2 g 1 p 2 102 p 3 3 g 1 g 2 g 3
|
||||
*
|
||||
* 输出: 1 1 -1 3 .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-04-20:04
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
Op ops;
|
||||
D d = new D(n);
|
||||
while (scanner.hasNextLine()) {
|
||||
String[] tmp = scanner.nextLine().split(" ");
|
||||
char op = tmp[0].charAt(0);
|
||||
int key = Integer.parseInt(tmp[1]);
|
||||
int val = 0;
|
||||
if (tmp.length > 2) {
|
||||
val = Integer.parseInt(tmp[2]);
|
||||
}
|
||||
ops = new Op(op, key, val);
|
||||
if (ops.op == 'p') {
|
||||
d.put(ops.key, ops.val);
|
||||
} else if (ops.op == 'g') {
|
||||
int res = d.get(ops.key);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class D {
|
||||
|
||||
int size;
|
||||
Map<Integer, Integer> map;
|
||||
Queue<Integer> queue = new LinkedList<>();
|
||||
|
||||
public D(int size) {
|
||||
this.size = size;
|
||||
map = new HashMap<>();
|
||||
}
|
||||
|
||||
public void put(int key, int val) {
|
||||
if (map.size() <= size) {
|
||||
map.put(key, val);
|
||||
queue.add(key);
|
||||
} else {
|
||||
int k = queue.poll();
|
||||
map.remove(k);
|
||||
map.put(key, val);
|
||||
queue.remove(key);
|
||||
queue.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
public int get(int key) {
|
||||
int res = map.getOrDefault(key, -1);
|
||||
queue.remove(key);
|
||||
queue.add(key);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
private static class Op {
|
||||
|
||||
char op;
|
||||
int key;
|
||||
int val;
|
||||
|
||||
public Op(char op, int key, int val) {
|
||||
this.op = op;
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
public Op() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
20
code/src/main/java/com/raorao/java/swing/HelloSwing.java
Normal file
20
code/src/main/java/com/raorao/java/swing/HelloSwing.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.raorao.java.swing;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-04-9:40
|
||||
*/
|
||||
public class HelloSwing {
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame("hello swing");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setSize(300, 100);
|
||||
frame.setVisible(true);
|
||||
|
||||
}
|
||||
}
|
32
code/src/main/java/com/raorao/java/thread/SemaphoreTest.java
Normal file
32
code/src/main/java/com/raorao/java/thread/SemaphoreTest.java
Normal file
@ -0,0 +1,32 @@
|
||||
package com.raorao.java.thread;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-09-04-17:30
|
||||
*/
|
||||
public class SemaphoreTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Semaphore semaphore = new Semaphore(3);
|
||||
ExecutorService service = Executors.newCachedThreadPool();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
service.submit(() -> {
|
||||
try {
|
||||
semaphore.acquire();
|
||||
System.out.println("可用线程数:" + semaphore.availablePermits());
|
||||
} catch (InterruptedException e1) {
|
||||
e1.printStackTrace();
|
||||
} finally {
|
||||
semaphore.release();
|
||||
}
|
||||
});
|
||||
}
|
||||
service.shutdown();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user