import java.util.Scanner; public class Test { int time = 0; public static void main(String[] args) { Test test = new Test(); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); System.out.println(test.dfs(num)); } public int dfs(int num){ if(num==0) return num; for (int i = 0; i < num; i++) { int min = (int)Math.pow(2,i),max = (int)Math.pow(2,i+1); int left = num-min,right = max-num; if(min <= num && max >= num){ time++; if(min==num||max==num){ return time; }else if(left == right && left!=0){ dfs(left); }else if((left)<(right)){ dfs(left); }else if((left)>(right)){ dfs(right); }else return time; } } return time; } } 这是我替同学做的,现在也不知道对不对,最后也没测试,不知道可不可以全过