解题思路:
主要观察分子分母的变化规律。
注意事项:
分子分母变化时注意采用临时变量
参考代码:
public static void main(String[] args) { Scanner input = new Scanner(System.in); int N = input.nextInt(); //输入项的个数 float a = 2; //分子 float b = 1; //分母 float result = 0; //接受记结果 for(int i=0 ; i<N ;i++){ result+=a/b; //第一项为2/1 float temp = a; //将a存在临时变量temp中 a+=b; //按照规律,分母为:分子+分母 b=temp;//将存在临时变量temp的值赋值给b } //保留两位小数输出 System.out.println(String.format("%.2f",result)); //关闭输入流 input.close(); }
0.0分
0 人评分