目录
题目:切面条
一根高筋拉面,中间切一刀,可以得到2根面条。
如果先对折1次,中间切一刀,可以得到3根面条。
如果连续对折2次,中间切一刀,可以得到5根面条。 那么,连续对折10次,中间切一刀,会得到多少面条呢?
这是一道选择题:
题目给出了四个答案:分别是1025、1024、513、512。
解题思路:
对折1次的结果是3,像是2的1次方+1也就是2^0,x=2+2^0;
对折2次的结果是5,像是2的2次方+1,x=2+2^0+2^1;
对折3次的结果,果然是9,像是2的3次方+1,x=2+2^0+2^1+2^2;
初步确定规律是 对折n次,得到2的n次方+1数量的面条,x=2+2^0+2^1+2^2……2^n;
对照java代码1 :利用Math类
package com.java.cs;
import java.util.Scanner;
public class MT {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
sc.close();
// 通过实验得到以下规律,+1是因为对折一次的时候是个单数
// Math.pow() 函数返回基数()的指数()次幂
int a = (int) Math.pow(2, x) + 1;
System.out.println(a);
}
}
对照代码2:for循环暴力破解
package com.java.cs;
import java.util.Scanner;
public class MtBl {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
int result = 2;//底数
int temp = 1;//用于缓存
// 循环累加公式: 2 + 2^0 + 2^1 + 2^2 + ...... + 2^n
for (int i = 0; i < n; i++) {
result += temp;
temp = temp * 2;//2的n次方,进行累加
}
System.out.println(result);
}
}
得出的答案:
所以这道题选A
总结:
本题目有很多种解法,无论是用手画,或者挨个计算,或者使用暴力循环,像我一样使用Math.pow() 函数返回基数()的指数()次幂方法也是可以的