选择题
class Person{
//堆
public String name;
public int age;
public double weight;
//方法区
public void eat(){
System.out.println(name+"eat()");
}
}
public class TestDemo2 {
public static void main(String[] args) {
//栈
Person p1=new Person();
Person p2=new Person();
}
}
编程题
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
int c = n*3;
int[] res = new int[c];
for(int i=0;i<c;i++)
res[i] = sc.nextInt();
Arrays.sort(res);
long sum = 0;
int s = res.length;
for(int i=0;i<n;i++) {
sum += res[s-2*(i+1)];
}
System.out.println(sum);
sc.close();
}
}
1 先从小到大进行排序
2 然后去把(数组长度减去2乘以(i+1)下标的数字求和)
3 对i进行遍历的时候是从0开始遍历到n-1
4 sum要用long来定义(int不可以)
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = sc.nextLine();
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s2.length(); i++) {
char c = s2.charAt(i);
map.put(c, map.getOrDefault(c, 0) + 1);
}
for (int i = 0; i < s1.length(); i++) {
if (map.get(s1.charAt(i)) == null)
System.out.print(s1.charAt(i));
}
sc.close();
}
}
1 注意读入换行是否要加sc.nextLine()(这个题目不可以加)
2 想起使用Map来记录字符串2中出现的字符
3 遍历字符串1来判断是否是字符串2中出现的字符
4 map的get方法判断的时候没有出现为null而不是0