京东笔试 对角线邻居 01问题 AC 73 73
对角线邻居 73%
import java.lang.reflect.Array;
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 = Integer.parseInt(sc.next());
int[] x = new int[n];
int[] y = new int[n];
sc.nextLine();
for(int i = 0 ; i < n ; i++){
String[] inS = sc.nextLine().split(" ");
x[i] = Integer.parseInt(inS[0]);
y[i] = Integer.parseInt(inS[1]);
}
int res = 0 ;
for (int i = 0 ; i < n ; i ++){
for (int j = i+1 ; j < n ; j++){
if (y[i] - y[j] ==0) continue;
if (x[i] - x[j] ==0) continue;
if (Double.valueOf(Math.abs(y[i]-y[j]))/Double.valueOf(Math.abs(x[i]-x[j]))==1) {
res++;
}
}
}
System.out.println(res);
}
}
01问题 73%
import java.util.*;
public class Main1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String inS = sc.next();
int[][] dp = new int[2][n];
int countZero = 0 ;
int countOne = 0;
for (int i = 0 ; i < n ; i ++){
if (inS.charAt(i) == '0') {
countZero += 1;
dp[0][i] = countZero;
dp[1][i] = countOne;
}
else {
countOne += 1;
dp[0][i] = countZero;
dp[1][i] = countOne;
}
}
int[] res = new int[n];
Arrays.fill(res,1);
for (int i = 0 ; i < n ; i++){
if (dp[0][i] == 0 ||dp[1][i] == 0) {
res[i] = i + 1;
continue;
}
for (int j = i-1 ; j >= 0 ; j--){
if ( dp[0][j]!=0&&dp[1][j]!=0 &&Double.valueOf(dp[0][i])/Double.valueOf(dp[1][i]) == Double.valueOf(dp[0][j])/Double.valueOf(dp[1][j])){
res[i]++;
}
}
}
for (int resOut : res){
System.out.print(resOut+" ");
}
}
}
#京东笔试##笔试题目##京东#