第一周
week1
day1
选择题
要使对象具有序列化能力,则其类应该实现如下哪个接口()。
A java.io.Serializable
B java.lang.Cloneable,
C java.lang.CharSequence
D java.lang.Comparable
选A
- Serializable接口是启用其序列化功能的接口。
- 拷贝功能的类实现Cloneable接口,并重写clone()方法,就可以通过调用clone()方法的方式简洁地实现实例 拷贝功能
- CharSequence是一个描述字符串结构的接口,子类有Stirng类StringBuffer类StringBuilder类
- Comparable接口强行对实现它的类的每个实例进行自然排序,该接口的唯一方法compareTo方法被称为自然比较方法
Math.round(11.5) 等于多少 (). Math.round(-11.5) 等于多少 ( ).
A 11 ,-11
B 11 ,-12
C 12 ,-11
D 12 ,-12
选C
round()方法可以这样理解:
将括号内的数+0.5之后,向下取值,
比如:round(3.4)就是3.4+0.5=3.9,向下取值是3,所以round(3.4)=3;
round(-10.5)就是-10.5+0.5=-10,向下取值就是-10,所以round(-10.5)=-10
组队竞赛
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[3*n];
for(int i = 0; i < 3*n; i++) {
arr[i] = in.nextInt();
}
Arrays.sort(arr);
long sum = 0;
for(int i = n; i < 3*n; i+=2) {
sum += arr[i];
}
System.out.println(sum);
}
}
删除公共字符
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String a = cin.nextLine();
String b = cin.nextLine();
char[] c = a.toCharArray();
StringBuilder s = new StringBuilder();
for(int i = 0; i < c.length; i++) {
if(!b.contains(c[i]+"")){
s.append(c[i]);
}
}
System.out.println(s.toString());
}
}
day2
选择题
选D
继承是有传递性的,所以代码没有问题
这里toLowerCase()这个方法返回的是一个new String,所以两者是不同的
选A
这里只想当给Test起了个别名,他是静态方法,本来用的是Test.但是这里用别名的手段也是没问题的
选D
抽象方法本来就是声明出来让子类来实现的,所以不能有方法具体内容,也就不能有{ }
选B
这里暂时还没有到,记住就好
排序子序列
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
//0:需要判断 1:递增 2:递减
int sta = 0;
int count = 0;
int j = 0;
while(j < n) {
j++;
while(j < n) {
if(arr[j] > arr[j-1]){
sta = 1;
break;
}else if(arr[j] < arr[j-1]) {
sta = 2;
break;
}else {
j++;
}
}
for(j = j+1 ; j < n; j++) {
if(sta == 2 && arr[j] > arr[j-1]) {
count++;
break;
}
if(sta == 1 && arr[j] < arr[j-1]) {
count++;
break;
}
}
}
System.out.println(count+1);
}
}
倒置字符串
import java.util.Scanner;
public class Main {
public static void reverse(char[] c,int i,int j) {
while(i < j) {
char tmp = c[i];
c[i] = c[j];
c[j] = tmp;
i++;
j--;
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String s = cin.nextLine();
s = s.trim();
char[] c = s.toCharArray();
reverse(c,0,c.length-1);
int start = 0;
for(int i = 0; i < c.length; i++) {
if(c[i] == ' ') {
reverse(c,start,i-1);
start = i+1;
}
}
reverse(c,start,c.length-1);
System.out.println(new String(c));
}
}
day3
选择题
选D
super 表示获取到父类实例的引用
子类继承了父类,子类构造的时候,需要首先帮助父类进行构造
怎么帮助构造,在子类的构造方法内部,调用父类的构造方法
字符串中找出连续最长的数字串
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String s = cin.nextLine();
int i = 0;
int max = 0;
int left = 0;
int right = 0;
while(i < s.length()) {
if(s.charAt(i) >= '1' && s.charAt(i) <= '9') {
int j = i;
while(true) {
if(j < s.length() && s.charAt(j) >= '1' && s.charAt(j) <= '9') {
j++;
}else{
break;
}
}
if(max < j-i) {
max = j-i;
left = i;
right = j;
}
i = j;
}else{
i++;
}
}
for(int j = left; j < right; j++) {
System.out.print(s.charAt(j));
}
}
}
数组中出现次数超过一半的数字
import java.util.*;
public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
Arrays.sort(array);
int i = 0;
int j = array.length/2;
while(j < array.length) {
if(array[i] == array[j]) {
return array[i];
}else{
i++;
j++;
}
}
return 0;
}
}
day4
选择题
arraycopy底层C/C++代码写的,效率最高
计算糖果
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int a = cin.nextInt();
int b = cin.nextInt();
int c = cin.nextInt();
int d = cin.nextInt();
if((a+c)%2!=0 || (b+d)%2!=0) {
System.out.println("No");
}else {
int A = (a+c)/2;
int B = (b+d)/2;
int C = d-B;
if(A < 0 || B < 0 || C < 0) {
System.out.println("No");
}else {
System.out.print(A+" "+B+" "+C);
}
}
}
}
进制转换
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int m = cin.nextInt();
int n = cin.nextInt();
boolean zheng = true;
if(m < 0) {
zheng = false;
m = -m;
}
if(n < 10) {
Stack<Integer> stack = new Stack<>();
while (m != 0) {
stack.push(m%n);
m = m/n;
}
int size = stack.size();
if(!zheng) {
System.out.print("-");
}
for(int i = 0; i < size; i++) {
System.out.print(stack.pop());
}
}else {
Stack<Character> stack = new Stack<>();
while (m != 0) {
int tmp = m%n;
if(tmp == 10) stack.push('A');
if(tmp == 11) stack.push('B');
if(tmp == 12) stack.push('C');
if(tmp == 13) stack.push('D');
if(tmp == 14) stack.push('E');
if(tmp == 15) stack.push('F');
if(tmp < 10){
stack.push((char) ('0'+tmp));
}
m = m/n;
}
int size = stack.size();
if(!zheng) {
System.out.print("-");
}
for(int i = 0; i < size; i++) {
System.out.print(stack.pop());
}
}
}
}
day5
选择题
1 //自动装箱
2 Integer total = 99;
3
4 //自动拆箱
5 int totalprim = total;
统计回文
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a = in.nextLine();
String b = in.nextLine();
int count = 0;
for(int i = 0; i <= a.length(); i++) {
StringBuilder s = new StringBuilder();
for(int m = 0; m < i; m++) {
s.append(a.charAt(m));
}
s.append(b);
for(int n = i; n < a.length(); n++) {
s.append(a.charAt(n));
}
if(isReverse(s)) {
count++;
}
}
System.out.println(count);
}
private static boolean isReverse(StringBuilder s) {
String str = s.toString();
int i = 0;
int j = str.length()-1;
while(i < j) {
if(str.charAt(i) != str.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}
连续最大和
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
int[] dp = new int[n];
dp[0] = Math.max(arr[0], 0);
int max = 0;
for(int i = 1; i < n; i++) {
dp[i] = Math.max(dp[i - 1] + arr[i], 0);
if(max < dp[i]) {
max = dp[i];
}
}
if(max == 0) {
int downMax = arr[0];
for(int i = 1; i < n; i++) {
if(downMax < arr[i]) {
downMax = arr[i];
}
}
System.out.println(downMax);
}else {
System.out.println(max);
}
}
}
day6
选择题
不要二
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int hang = scanner.nextInt();
int lie = scanner.nextInt();
int[][] table = new int[hang][lie];
//1表示蛋糕,2表示不能放的格子,0代表可以放
int i = 0;
int j = 0;
int count = 0;
while(i < hang) {
if(table[i][j] == 0) {
table[i][j] = 1;
count++;
if(i+2 < hang) {
table[i+2][j] = 2;
}
if(j+2 < lie) {
table[i][j+2] = 2;
}
}
if(++j >= lie) {
j = 0;
i++;
}
}
System.out.println(count);
}
}
把字符串转换成整数
public class Solution {
public int StrToInt(String str) {
if(str == null || str.equals("")) return 0;
char[] ch = str.toCharArray();
int count = 0;
int sum = 0;
for(int i = ch.length-1; i > 0; i--) {
if (ch[i] >= '0' && ch[i] <= '9') {
int tmp = ch[i]-'0';
sum += tmp * (int)Math.pow(10,count);
count++;
}else {
return 0;
}
}
if(ch[0] == '+') {
return sum;
}else if(ch[0] == '-'){
sum = -sum;
}else {
int tmp = ch[0]-'0';
sum += tmp * (int)Math.pow(10,count);
}
return sum;
}
}