- 机器人的运动范围
- 剪绳子
注意j的取值判断是i的一半
public static int cut(int n){
int[] res = new int[n+1];
int i=0;
while(i<4){
res[i]=i;
i++;
}
for(i=4;i<=n;i++){
int max=0;
for(int j=1;j<i/2;j++){
int temp = res[j]*res[i-j];
res[i]=Math.max(temp, max);
}
}
return res[n];
}
- 二进制中 1 的个数
public static int NumberOf1(int n){
int cnt=0;
while(n!=0){
n&=n-1;
cnt++;
}
System.out.println(cnt);
return cnt;
}
- 打印从 1 到最大的 n 位数