问题 1066: 2004年秋浙江省计算机等级考试二级C 编程题(2)
輸入一個正數x和一個正整數n,求下列算式的值。要求定義兩個調用函數:fact(n)計算n的階乘;mypow(x,n)計算x的n次冪(即xn),兩個函數的返回值類型是double。
????? x - x2/2! + x3/3! + ... + (-1)n-1xn/n!
?
×輸出保留4位小數。
?
輸入x n
輸出數列和
樣例輸入 2.0 3 樣例輸出 1.3333import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
double x = in.nextDouble();
int n = in.nextInt();
double y=0.0;
double sum=0.0;
for(int i=1;i<=n;i++){
y=Math.pow(-1, i-1)*mypow(x,i)/fact(i);
sum+=y;
}
System.out.printf("%.4f", sum);
}
public static double fact(int n){
double a=1;
for(int i=1;i<=n;i++){
a=a*i;
}
return a;
}
public static double mypow(double x, int n){
return Math.pow(x, n);
}
}
轉載于:https://www.cnblogs.com/xuesujun/p/11355598.html
總結
以上是生活随笔為你收集整理的问题 1066: 2004年秋浙江省计算机等级考试二级C 编程题(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 问题 1052: [编程入门]链表合并
- 下一篇: 迅雷都可以支持什么格式播放迅雷看看支持视