1. 世纪末的星期
import java.util.Calendar;
public class Main {public static void main(String[] args) {Calendar calendar = Calendar.getInstance();for(int year = 1999;year<100000;year+=100){calendar.set(Calendar.YEAR,year);calendar.set(Calendar.MONTH,11);calendar.set(Calendar.DAY_OF_MONTH,31);if(calendar.get(Calendar.DAY_OF_WEEK)==1){System.out.println(year);break;}}}
}
2. 星期计算
public class Main {public static void main(String[] args) {int n = 20;for(int i = 0;i<21;i++){n = (20*n)%7;}System.out.println((n+5)%7+1);}
}
3. 更小的数
【解法一 模拟法 通过40%】
StringBuilder:内容是可变的,String:内容是不变的
public StringBuilder(String str):根据字符串的内容,来创建可变字符串对象。
public StringBuilder reverse():返回相反的字符序列。
public StringBuilder append(任意类型):添加数据,并返回对象本身。
public String toString():通过toString就可以实现把StringBuilder转换为String
public int length()返回对象内容长度
replace(int start, int end, String str):将起始位置为start,结束位置为end-1的子串替换为str。不生成新的StringBuilder对象,在原来的StringBuilder对象上修改。
String 字符串类型,它的比较值用compareTo方法,它从第一位开始比较,,如果遇到不同的字符,则马上返回这两个字符的ASCII码的差值,返回值是int类型
import java.util.*;
public class Main{public static void main(String[] args){Scanner sc = new Scanner(System.in);String str = sc.next();int count = 0;for(int i = 0;i<str.length();i++){for(int j = i+1;j<str.length();j++){StringBuilder s = new StringBuilder(str);s.replace(i,j+1,new StringBuilder(str.substring(i,j+1)).reverse().toString());if(s.toString().compareTo(str)<0){count++;}}}System.out.println(count);sc.close();}
}
【解法二 直接暴力 100%】
import java.util.*;
public class Main{public static void main(String[] args){Scanner sc = new Scanner(System.in);String str = sc.next();int count = 0;for(int i = 0;i<str.length();i++){for(int j = i+1;j<str.length();j++){for(int p = i,q=j;p<q;p++,q--){char a1 = str.charAt(p);char a2 = str.charAt(q);if(a1==a2) continue; if(a1>a2) count++;break;}}}System.out.println(count);sc.close();}
}
【解法三 DP】
import java.util.*;
public class Main{public static void main(String[] args){Scanner sc = new Scanner(System.in);String str = sc.next();int dp[][] = new int[5002][5002];int count = 0;for(int k = 1;k<str.length();k++){for(int i = 0;i+k<str.length();i++){int j = i+k;if(str.charAt(i) < str.charAt(j)) dp[i][j]=0;if(str.charAt(i) > str.charAt(j)) dp[i][j]=1;if(str.charAt(i) == str.charAt(j)) dp[i][j]=dp[i+1][j-1];if(dp[i][j]==1) count++;}}System.out.println(count);sc.close();}
}
4. 接龙数列
import java.util.*;
public class Main {public static void main(String[] args) {Scanner scan = new Scanner(System.in);int num = scan.nextInt();String arr[] = new String[num];for(int i = 0;i<num;i++){arr[i] = scan.next();}int dp[] = new int[10];for(int i = 0;i<num;i++){int head = arr[i].charAt(0)-'0';int tail = arr[i].charAt(arr[i].length()-1)-'0';dp[tail] = Math.max(dp[head]+1,dp[tail]);}int max = 0;for(int i = 0;i<10;i++){max=Math.max(max,dp[i]);}System.out.println(num-max);scan.close();}
}
5. 填充
import java.util.Scanner;
public class Main {public static void main(String[] args) {Scanner scan = new Scanner(System.in);char s[] = scan.next().toCharArray();int count = 0;for(int i = 0;i<s.length-1;){if(s[i]==s[i+1]||s[i]=='?'||s[i+1]=='?'){count++;i+=2;}else{i++;}}System.out.println(count);scan.close();}
}