阿里笔试 9.3
第一题: 输入字符串A,B 只有0、1组成,在[i,j] 范围中1变成0,0变成1,求最少需要多少次使得A变成B
样例 :
3
100
011
输出:1
说明 在[1,3] 中A 变换1次: 100 -> 011
3
101
000
2
AC public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine()); String A = scanner.nextLine(); String B = scanner.nextLine(); int j =0 ,count = 0; int [] dp = new int[n]; for (int i = 0; i < n; i++) { if (A.charAt(i)!=B.charAt(i)) dp[i] = 0; else dp[i] = 1; } for (int i = 0; i < n; i++) { if (dp[i] == 0) j++; else { if (j>0)count++; j=0; } } if (j>0)count++; System.out.println(count); } }
第二题:A、B、C 三个数 ,求 x,y 的组合次数,使得满足下面条件:
A<= x*x<=B
A<= y*y*y<=B
|x*x - y*y*y| <=c
正确率: 21.67% public class Main02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double a = scanner.nextDouble(); double b = scanner.nextDouble(); double c = scanner.nextDouble(); double i = Math.sqrt(a); double j = Math.sqrt(a); long count = 0; for (;i<Math.sqrt(b);i++){ for (;j*j*j<b;j++) if (Math.abs(i*i-j*j*j)<=c) count++; } String res = String.valueOf(count); System.out.println(res.substring(0,res.length()-2)); } }