Description 输入n的值,计算cos(x)。
Input 输入数据有多行,每行两个数,包括x和n。第一数据为x,第二个数据为n。
Output 输出cos(x)的值,保留4位小数。
Sample Input 0.0 100 1.5 3 Output 1.0000 0.0701
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner reader = new Scanner(System.in); double cos, x, fenmu; int n, j, i; while (reader.hasNext()) { cos = 1; x = reader.nextDouble(); n = reader.nextInt(); for (i = 1; i <= n; i++) { fenmu = 1; for (j = 1; j <= 2 * i; j++) { fenmu = fenmu * j; } cos = cos + Math.pow(-1, i) * Math.pow(x, 2 * i) / fenmu; } System.out.printf("%.4f\n", cos); } } }