T1 转盘锁(100%)
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String[] strs = str.split(" ");
int res = 0;
for(int i = 0; i < strs[0].length(); i++){
int num1 = Integer.valueOf(strs[0].charAt(i)), num2 = Integer.valueOf(strs[1].charAt(i));
res += (num1 >= num2 ? (num1 - num2) : (num1 + 10 - num2));
}
System.out.println(res);
}
}
T2 连续的不嘲笑子段(100%)
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
int[] b = new int[n];
int tag = 0;//是否选择第一个
for(int i = 0; i < n; i++) a[i] = in.nextInt();
for(int i = 0; i < n; i++) b[i] = in.nextInt();
int res = 1, cur = 0;
for(int i = 1; i < n; i++){
int sub1 = a[i] - a[i-1], sub2 = b[i] - b[i-1];
if(sub1 == sub2){
if(cur++ != i) tag = -1;
res = Math.max(res, cur);
continue;
}
cur = 0;
}
System.out.println(tag == 0 ? res : (res + 1));
}
}
T3 最长括号匹配前缀(100%)
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
String s = in.nextLine();
int res = 0, l = 0, r = 0;
for(int i = 0; i < n; i++){
if(s.charAt(i) == '(') l++;
else r++;
if(l == r) res = i+1;
if(r > l) break;
}
System.out.println(res);
}
}