数列的定义如下:数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。
import java.util.*; public class Main { private static final int MAX = 1005; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { double n = sc.nextDouble(); int m = sc.nextInt(); double ans = 0; for (int i = 0; i < m; i++) { ans += n; n = Math.sqrt(n); } System.out.printf("%.2f\n", ans); } } }
#include<iostream> #include<stdio.h> #include<math.h> using namespace std; int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF){ double sum=0; double a=n; for(int i=0;i<m;i++) { sum+=a; a=sqrt(a); if(a<0.0001) break; //因为精度要求保留2位小数,所以此时后面的位数可以不用了 } printf("%.2f\n",sum); } return 0; }
/* 二分法求根号x 求根号具备二分法的单调满足特性,所以可以用二分来求。 每二分一次相当于除以1个2,二分次数越多,精度越高。 二分10次=1/(2^10)=1/1000=0.001,即能精确到3位有效数字 二分50次=1/(2^50)=1/(10^15):能精确到15位左右有效数字(double精度极限) */ double sqrt(double x){ //在区间[0,x]上找根号x double L=0,R=x+0.1,mid; //二分次数越多,区间[l,r]就越小,精度就越高,结果L就越逼近根号x for(int i=0;i<50;i++){//本题居然要二分50次左右才够,接近double精度(16位左右)极限! mid=(L+R)*0.5; if(mid*mid<=x) L=mid;//判断mid平方是否接近x,太小则去大区间上搜索 else R=mid;//过大则缩小 } return L; }
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m; double n, result; // sc.hasNext() : 当控制台输入不是"-"时,就将其压入栈中,当输入为"-",栈不为空则弹出。 while (sc.hasNext()){ n = sc.nextInt(); m = sc.nextInt(); result = 0; for (int i = 0 ; i < m; i++){ result += n; n = Math.sqrt(n); } System.out.println(String.format("%.2f", result)); } } }
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner in=new Scanner(System.in); while(in.hasNext()) { double N=in.nextDouble(); //数据 int M=in.nextInt(); //前m个 double result=0; //和 for(int i=0;i<M;i++) { result+=N; //第一个数是N本身 N=Math.sqrt(N); } System.out.printf("%.2f\n",result); } } }
import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static double sum(int m,int n){ double total=0; double cur=m; int cnt=0; while(cnt<n){ total+=cur; cur=Math.sqrt(cur); cnt++; } return total; } public static void main(String[] args) throws IOException { //因为不知道Scanner有hasNext()方法,所以参考了一下之前提交的用Buffered的方法 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str=null; while((str=br.readLine())!=null){ String[] strs=str.split(" "); int num1=Integer.parseInt(strs[0]); int num2=Integer.parseInt(strs[1]); //这里用String的处理方法比较方便,当然也可以使用DecimalFormat之类的来完成,涨知识了 System.out.println(String.format("%.2f", Main.sum(num1,num2))); } } }
#include <stdio.h> #include <math.h> int main() { int m, n; while(scanf("%d%d", &n, &m)!= EOF) { double n1 = n; double sum = 0; for (int i = 1; i <= m; i++) { sum += n1; n1 = sqrt(n1); } printf("%.2lf\n", sum); } return 0; }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int n = sc.nextInt(); int m = sc.nextInt(); double tmp = n, sum = 0; while (m-- != 0) { sum += tmp; tmp = Math.sqrt(tmp); } // 用四舍五入保留整数的方式来保留小数 sum = (Math.round(sum * 100)) / 100.0; System.out.println(sum); } } }
while(str = readline()){ var lines = str.split(' '); var total =0; var x =Number(lines[0]); for(let i =0 ;i<lines[1];i++){ total += x; x=Math.sqrt(x); } total = (total).toFixed(2); console.log(total) }
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { int n, m; while (cin >> n >> m) { double sum = n; double sqrt_ = sqrt(n); for (int i = 0; i < m-1; i++) { sum += sqrt_; sqrt_ = sqrt(sqrt_); } cout << fixed << setprecision(2) << sum << endl; } }
package main import ( "errors" "fmt" "io" ) func main() { var n, m int var num int var err error num, err = fmt.Scanln(&n, &m) for num != 0 && err != errors.New(io.EOF.Error()) { sum := 0.0 var tmp float64 = float64(n) //求tmp平方根 for i := 0; i < m; i++ { sum += tmp tmp = mySqrt(tmp) if tmp < 0.0001 { goto next //精度要求哦i } } fmt.Printf("%.2f\n", sum) next: num, err = fmt.Scanln(&n, &m) } } //牛顿迭代法 f(x)=x*x - C 求零点 func mySqrt(x float64) float64 { y, check := 1.0, 0.0 fx := float64(x) y = (fx/y + y) / 2.0 check = y*y - fx for check > 0.001 { y = (fx/y + y) / 2.0 check = y*y - fx } return y }