余弦 (java实现)

Problem Description

输入n的值,计算cos(x)。

Input

输入数据有多行,每行两个数,包括x和n。第一数据为x,第二个数据为n。

Output

输出cos(x)的值,保留4位小数。

Sample Input

0.0 100
1.5 3

Sample Output

1.0000
0.0701

Hint

 

Source

 1 import java.util.*;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         while (sc.hasNext()){
 7             double sum = 1.0;
 8             double x = sc.nextDouble();
 9             double n = sc.nextDouble();  // 注意精度
10             for (int i = 1; i <= n; i++) {
11                 double mul = 1;
12                 for(int j = 1; j <= 2*i; j++){
13                     mul *= j;
14                 }
15                 sum += Math.pow(-1,i)*Math.pow(x,2*i)/mul;
16             }
17             System.out.printf("%.4f
", sum);
18         }
19     }
20 }
原文地址:https://www.cnblogs.com/sugerandmaster/p/11502707.html