度小满笔试题(Android岗位100%和64%)
求隔山打牛正确思路!!!
第一题:棋盘遍历:100%
public class one { public static void main(String[] args) { Scanner read = new Scanner(System.in); long n = read.nextLong(); long m = read.nextLong(); if(n == 1 || m == 1){ if(n == 1){ System.out.println(m - 1 + m - 1 ); }else{ System.out.println(n - 1 + n - 1); } } else if(n % 2 == 0 || m % 2 == 0){ System.out.println(n*m); }else{ System.out.println(n * m + 1); } } }
第二题:隔山打牛:64%
public class two { public static void main(String[] args) { Scanner read = new Scanner(System.in); int n = read.nextInt(); int[] num = new int[n+1]; for(int i = 1;i<=n;i++){ num[i] = read.nextInt(); } int max; int sum = 0; for(int i=n;i>=1;i--){ if(num[i] > 0){ sum+=num[i]; //奇数的 if(i - 1 > 0 && (( i - 1) % 2 ) == 0){ num[i - 1] -= num[i]; num[(i - 1) / 2] -= num[i]; }else if(i != 1){ // 偶数的 num[ i / 2 ] -= num[i]; } } } System.out.println(sum); } }