题目
解析
1、不对折(对折零次),从中间切一刀,得到 2 根面条, 2 = 2
2、对折一次,从中间切一刀,得到 3 根面条, 3 = 2 + 2^0
3、对折两次,从中间切一刀,得到 5 根面条, 5 = 2 + 2^0 + 2^1
4、对折三次,从中间切一刀,得到 9 根面条, 9 = 2 + 2^0 + 2^1 + 2^2
…
设面条数为x,折数为y,可以得出规律 : x=2^y+1
代码如下
package 切面条;
import java.util.Scanner;
public class 切面条 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1,数学方法
Scanner sc = new Scanner(System.in);//输入切面条次数
int x = sc.nextInt();//控制台输入
sc.close();
int y = (int) Math.pow(2, x) +1;
System.out.println(y);
// 2,for循环
int y1 = 2;
int x1 = 1;
for(int i = 0;i<10;i++) {
y1 += x1;
x1=x1*2;
}
System.out.println(y1);
}
}
结果如下