1.如果希望监听TCP端口9000,服务器端应该怎样创建socket? B
A.new Socket(“localhost”,9000);
B.new ServerSocket(9000);
C.new Socket(9000);
D.new ServerSocket(“localhost”,9000);
2.jre 判断程序是否执行结束的标准是(A)
A.所有的前台线程执行完毕
B.所有的后台线程执行完毕
C.所有的线程执行完毕
D.和以上都无关
3.下列哪项不属于jdk1.6垃圾收集器?D
A.Serial收集器
B.parNew收集器
C.CMS收集器
D.G1收集器
4.标题:洗牌
洗牌在生活中十分常见,现在需要写一个程序模拟洗牌的过程。 现在需要洗2n张牌,从上到下依次是第1张,第2张,第3张一直到第2n张。首先,我们把这2n张牌分成两堆,左手拿着第1张到第n张(上半堆),右手拿着第n+1张到第2n张(下半堆)。接着就开始洗牌的过程,先放下右手的最后一张牌,再放下左手的最后一张牌,接着放下右手的倒数第二张牌,再放下左手的倒数第二张牌,直到最后放下左手的第一张牌。接着把牌合并起来就可以了。 例如有6张牌,最开始牌的序列是1,2,3,4,5,6。首先分成两组,左手拿着1,2,3;右手拿着4,5,6。在洗牌过程中按顺序放下了6,3,5,2,4,1。把这六张牌再次合成一组牌之后,我们按照从上往下的顺序看这组牌,就变成了序列1,4,2,5,3,6。 现在给出一个原始牌组,请输出这副牌洗牌k次之后从上往下的序列。
输入描述:
第一行一个数T(T ≤ 100),表示数据组数。对于每组数据,第一行两个数n,k(1 ≤ n,k ≤ 100),接下来一行有2n个数a1,a2,…,a2n(1 ≤ ai ≤ 1000000000)。表示原始牌组从上到下的序列。
输出描述:
对于每组数据,输出一行,最终的序列。数字之间用空格隔开,不要在行末输出多余的空格。
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int num = sc.nextInt();
while(num!=0){
int n = sc.nextInt();
int k = sc.nextInt();
int[] array = new int[2*n];
for(int i = 0;i<2*n;i++){
int index = i;
for(int j = 0;j<k;j++){
if(index<n){
index = index*2;
}else{
index = (index-n)*2+1;
}
}
array[index] = sc.nextInt();
}
for(int i = 0;i<2*n;i++){
if(i==2*n-1){
System.out.print(array[i]);
}else{
System.out.print(array[i]+" ");
}
}
System.out.println();
num--;
}
}
}
}
//方法二:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int groups = sc.nextInt();
//数组组数
while (groups-- > 0) {
int n = sc.nextInt();
//数字个数的一半
int k = sc.nextInt();
//洗牌次数
int[] res = new int[2 * n];
//数据总个数
for (int i = 0; i < 2 * n; i++) {
//遍历数据数组
int tmp = i + 1;
//此处修正内层循环更加temp计算位置
for (int j = 0; j < k; j++) {
if (tmp <= n) {
tmp = 2 * tmp - 1;
} else {
tmp = 2 * (tmp - n);
}
}
res[tmp - 1] = sc.nextInt();
//数组中temp-1的位置的数据,即就是洗牌后的结果
}
//输出
if (res.length > 0) {
System.out.print(res[0]);
}
for (int i = 1; i < 2 * n; i++) {
System.out.print(" " + res[i]);
}
System.out.println();
}
}
}
5.标题:统计同成绩学生人数
读入N名学生的成绩,将获得某一给定分数的学生人数输出。
输入描述:
测试输入包含若干测试用例,每个测试用例的格式为
第1行:N
第2行:N名学生的成绩,相邻两数字用一个空格间隔。
第3行:给定分数当读到N=0时输入结束。其中N不超过1000,成绩分数为(包含)0到100之间的一个整数。
输出描述:
对每个测试用例,将获得给定分数的学生人数输出。
示例1:
输入
3
80 60 90
60
2
85 66
0
5
60 75 90 55 75
75
0
输出
1
0
2
import java.util.*;
public class Main{
private static int Solution(String[] s,int grade){
int result = 0;
List<Integer> list = new ArrayList<>();
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0;i<s.length;i++){
list.add(Integer.parseInt(s[i]));
}
for(int i = 0;i<list.size();i++){
int num = list.get(i);
int count = map.getOrDefault(num,0);
map.put(num,count+1);
}
if(map.containsKey(grade)){
result = map.getOrDefault(grade,0);
}else{
result = 0;
}
return result;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int flag = 1;
while (flag!=0){
while(sc.hasNext()){
int count = sc.nextInt();
if (count != 0) {
sc.nextLine();
String line = sc.nextLine();
int grade = sc.nextInt();
String[] s = line.split(" ");
int result = Solution(s,grade);
System.out.println(result);
}else{
flag = count;
break;
}
}
}
}
}
//方法二:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int N = scan.nextInt();
// 输入的人数
if (N == 0) {
// 如果检测到输入的人数为0 则结束
return;
}
int[] arr = new int[N];
// 保存N个人的分数
for (int i = 0; i < arr.length; i++) {
arr[i] = scan.nextInt();
}
int t = scan.nextInt();
// 目标分数 输出击中目标分数的个数
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (t == arr[i]) {
count++;
}
}
System.out.println(count);
}
}
}