public static void main(String[] args) { int max = (1 << 10) - 1; BigInteger TWO = BigInteger.valueOf(2); BigInteger tmp = new BigInteger("0"); BigInteger sum = tmp; for (int i = 0; i < max; i++) { tmp = tmp.multiply(TWO).add(BigInteger.ONE); System.out.println(tmp); sum = sum.add(tmp); } System.out.println(sum); }一直加到2的10次方,数字太大了... 前面加!标示对逻辑运行求反,把true变成false; false变成true.
int max = (1 << 10) - 1;
BigInteger TWO = BigInteger.valueOf(2);
BigInteger tmp = new BigInteger("0");
BigInteger sum = tmp;
for (int i = 0; i < max; i++) {
tmp = tmp.multiply(TWO).add(BigInteger.ONE);
System.out.println(tmp);
sum = sum.add(tmp);
}
System.out.println(sum);
}一直加到2的10次方,数字太大了... 前面加!标示对逻辑运行求反,把true变成false; false变成true.
所以true的否定(!)就是false了..
for(int i=1;i<=10;i++)
{
result+=Math.pow(2,i)-1.0;
}System.out.println(result);
int result=0;
for(int i=1;i <=10;i++)
{
result += (1 << i) - 1;
} System.out.println(result);
前面的每个数先+1 最后再减10
。。
这道数学题 so easy
楼主的题如果不用for循环可以用
return (1 << 11) - 12 ;
效率比for快多了
-------------------------------------
http://www.pkwutai.cn
(2^1-1)+(2^2-1)+……+(2^n-1)=2*(2^n-1)-n
n=10,所以:2*(2^10-1)-10=2^11-12LZ需要for,所以移位。long data = 1L;
for(int i=0;i<11;i++) {
data<<=1;
}
data-=12;