import java.util.Arrays; public class SouGou1 { public static void main(String[] args) { System.out.println(getPasswordCount("12345")); } //第一题,直接用数学方法算; public static int numberofprize(int a, int b, int c) { // write code here long[] arr = {a, b, c}; Arrays.sort(arr); long num1=arr[1]-arr[0]; long num2=arr[2]-arr[1]; if(num1*2<=num2){ long sum = arr[0] * 2 + arr[1]*2 + arr[2]; return (int)(sum / 5); }else{ long sum = arr[0] * 2 + arr[1] + arr[2]; return (int)(sum / 4); } } //第二题,没啥好说的 public int getHouses (int t, int[] xa) { // write code here int count=0; for(int i=0;i<xa.length-2;i+=2){ double start=xa[i]+(double)xa[i+1]/2; double end=xa[i+2]-(double)xa[i+3]/2; if(end-start==t) count++; else if(end-start>t) count+=2; } return count+2; } //第三题,我当时出BUG没搞出来,哎,现在写出来应该能AC,复杂度不算高,也没有用到额外的空间; public static long getPasswordCount (String password) { // write code here int count=0; for(int i=0;i<10;i++){ count+=dfs(password,1,i); } for(int i=0;i<password.length()-1;i++){ int pre=(int) password.charAt(i); int n=(int) password.charAt(i+1); double temp=(double)(n+pre)/2; if(temp!=Math.floor(temp)){ if((int)temp!=n&&(int)temp+1!=n) return count; }else { if((int)temp!=n) return count; } } return count-1; } private static long dfs(String passward,int index,int pre){ if(index>=passward.length()) return 1; double temp=(double)((int)passward.charAt(index)+pre)/2; if(temp==Math.floor(temp)) return dfs(passward,index+1,(int)temp); else{ return dfs(passward,index+1,(int)(temp))+dfs(passward,index+1,(int)(temp)+1); } } }
全部评论
(1) 回帖