难度##
很简单的一道题
题目
数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。
输入描述:
输入数据有多组,每组占一行,由两个整数n(n < 10000)和m(m < 1000)组成,n和m的含义如前所述。
输出描述:
对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留2位小数。
示例1
81 4
2 2
输出
94.73
3.41
过程
没有审题清楚,没有考虑到原数据小数只有一位或没有小数的情况!!!一开始没有格式化输出!!!
没有审题清楚,没有考虑到原数据小数只有一位或没有小数的情况!!!一开始没有格式化输出!!!
没有审题清楚,没有考虑到原数据小数只有一位或没有小数的情况!!!一开始没有格式化输出!!!
改了后就过了
注意事项
这里累加的是 输入值N 经过M次平方根的值 注意一下
源码
package com.niuketest0607;
import java.text.DecimalFormat;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
float prevalue = 0;
float nextvalue = 0;
int countnumber= 0;
float summation = 0;
while(true){
prevalue = input.nextInt();
if(prevalue>0&&prevalue<10000){
countnumber = input.nextInt();
if(countnumber>0&&countnumber<1000){
break;
}
}
}
for(int times=0;times<countnumber;times++){
nextvalue = Main.tosqrt(prevalue, times);
summation += nextvalue;
}
DecimalFormat decimalFormat=new DecimalFormat(".00");
String output = decimalFormat.format(summation);
System.out.print(output);
}
static float tosqrt(float readytosqrt,int timesofsqrt){
for(int j=0;j<timesofsqrt;j++){
readytosqrt = (float) Math.sqrt(readytosqrt);
}
return readytosqrt;
}
}