求平方根数列的和

难度##

很简单的一道题

题目

数列的第一项为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;
	}

}

发布了104 篇原创文章 · 获赞 264 · 访问量 54万+

猜你喜欢

转载自blog.csdn.net/teavamc/article/details/80607411