for循环语句之棋盘放粮食、百鸡百钱、纸张的折叠问题
1.棋盘放粮食
int lszl = 1; for (int i = 1; i < 30; i++) { int x = 1; for (int j = 1; j <= i; j++) { x = x * 2; } lszl = lszl + x; } double zl = lszl * 0.001; Console.WriteLine("放满整个棋盘所需要:" + zl + "kg的粮食");
2.百鸡百钱
for (int i = 1; i < 50; i++) { for (int j = 1; j < 100; j++) { for (int k = 1; k < 200; k++) { double a = i*2+j*1+k*0.5; if (a == 100) { Console.WriteLine("买大鸡:"+i+"只,小鸡"+j+"只,幼鸡"+k+"只"); } } } }
3.一张纸厚度是0.1毫米,假设这张纸可以限次对折,问对折几次可以超过珠峰?8848米
int a = 1; double z = 0.0001; for (int i = 1; i <= i+1; i++) { z = z * 2; if (z > 8848) { a = i; break; } } Console.WriteLine("折" + a + "次可以跟珠穆朗玛峰一样高");