in fact等于 如何定义函式fact(n) 计算n的阶乘:n!=1*2*……*n,函式返回值型别是double?
如何定义函式fact(n) 计算n的阶乘:n!=1*2*……*n,函式返回值型别是double?
2. 试题 (1) 定义函式fact(n) 计算n的阶乘:n!=1*2*……*n,函式返回值型别是double。
double?位数太少,来个狠的,要不要!最大可计算(10^9 -1)! 计算10000的阶乘只要0.5秒!(更正一下,计算10000!时,b资料型别可以设定为long,此时运算为0.5秒,设定为long long后耗时增加,也可能CPU速度快不要0.5秒)
雨中飞燕之作改写
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<time.h>
long t=2,a,m;
long long b;
main()
{
/ prepare /
int num = 0;
while(1){
printf("Input a natural number that you want to get its factorial.n");
scanf("%d",&num);
getchar();
if(num <0){
printf("Your Input is illegal!n");
}
else{
break;
}
}
int digit = 1;
int n = 10;
int i = num;
while(i /= 10){
digit++;
n *= 10;
}也可以固定n=1000000000(最大),实际测试表明n越大计算速度越快。
char output[8];
sprintf(output,"%%0%dld",digit);
/ ready
按sizeof(long)==4,sizeof(long long)==8算,
最大可计算(10^9 -1)! 要求记忆体够大哦^_^
long *s = (long *) malloc(sizeof(long)*num);
if(NULL == s){
perror("malloc");
return -1;
}
memset(s,0,sizeof(long)*num);
s[0] = 1; long s[num]={1};
double start,finish;
start = clock();
原语句简单化
for(t=2;t<=num;t++){
for(a=0;a<=m;a++){
s[a]=(b+=s[a]*t)%n,b/=n;
if( (m==a) && b)m++;
}
}
for(printf("%d!=%ld",num,s[m]);m--;)printf(output,s[m]);若固定n=1000000000则改为printf("%09ld",s[m]);
printf("n");
finish = clock();
printf("Spended %f seconds to calculate.n",(finish-start)/CLOCKS_PER_SEC);
free(s);
#if 0
FILE *fp;输出到档案,也要求磁碟空间够大^_^
if((fp = fopen("/example/save","w+"))==NULL){
printf("open save file errorn");
}
else{
for(fprintf(fp,"%ld",s[m]);m--;)fprintf(fp,output,s[m]);
}
free(s);
#endif
return 0;
}
/*功能独立出来
long fact(long num,long n,long *s)
{
long t,a;
long m = 0;
long long b = 0;
for(t=2;t<=num;t++){
for(a=0;a<=m;a++){
s[a]=(b+=s[a]*t)%n,b/=n;
if( (m==a) && b)m++;
}
}
return m;
}
*/
/*
#include<stdio.h> 雨中飞燕之作
#define N 1000 要计算的N
long s[N]={1},n=10000,t=2,a,b,m;main(){
for(;a<=m||++t<=N&&(a=b=0,1);m==a++&&b&&m++)
s[a]=(b+=s[a]*t)%n,b/=n;
for(printf("%d",s[m]);m--;)printf("%04d",s[m]);}
*/

定义函式f(n)计算n+(n+1)+(n+2)+……+(2n-1),函式返回值型别是double 。
#include "stdio.h"
double f(int n)
{
int i;
double sum=0;
for(i=1;i<=n;i++)
sum=sum+n;
n++;
return sum;
}
main()
{
scanf("%d",&n);
printf("%lf",f(n));
}
f(n)=(3n-1)(n)/2=3n²/2-n/2
定义函式f(n)计算n+(n+1)+……+(2n+1)函式返回值型别double
# include "stdio.h"
double f (int)
{ if( n > 0 );
double m= 0.0;
for(int i = 0; i <= n+1; ++i)
{m+= (n+i);}
return m;
}
void main ()
{ double s;
s=f(n);
printf("%f",s);
}
编写函式f,计算n的阶乘并返回,函式返回值型别为double.
void main()
{
double cal(double a,int b); 这两行提到main函式外去
double fact(int c);
如下
double cal(double a,int b);
double fact(int c);
void main()
{
scanf("%lf%d",&x,&n);因为是double的,所以用lf
double i,j=0;
int k; double是没有++的,只有char,int,long才有
for(k=0;k<=b;k++)
定义函式f(n)计算n+(n+1)+.(2n+1),函式返回值型别是double。求解程式编写
public class Test {
public static void main(String[] args) {
double n=10;
double result=fmethod(n);
System.out.println(result);
}
public static double fmethod(double n) {
double r=0;
for (double i = n; i < =(2*n+1); i++) {
r+=i;
}
return r;
}
}
定义函式total(n)计算1+2+.+n,定义fact(n)计算n!(n!=1*2*.*n),函式total的型别为int,函式fact的返回值类
public int total(int n){
int result = 0;
for(int i=1;i<=n;i++){
result +=i;
}
return result;
}
public int fact(int n){
int result = 1;
for(int i=1;i<=n;i++){
result *=i;
}
return result;
}
定义函式fun(x)计算x^3 2.0*x^2-3.9x+8,函式返回值型别为double
不知道你是两个表示式还是中间落了一个运算子,如果你的表示式是如下:
x^3 + 2.0*x^2-3.9x+8
那么java可以这样实现:
public double fun(double x){ double result = Math.pow(x, 3) + 2.0 * Math.pow(x, 2) - 3.9 * x + 8; return result; }