发布网友 发布时间:2023-07-18 14:32
共1个回答
热心网友 时间:2024-10-31 01:26
#include <iostream.h>
int func(int n)
{
if(n==1)
return 1;
if(n>1)
return n*func(n-1);
}
void main(int agrc,int **argv)
{
int N;
cout<<"Input the number:";
cin>>N;
int sum=func(N);
cout<<sum<<endl;
}
受计算机数据精度的*,只能计算到17的阶乘
Dim n As Integer
For n = 1 To 17
Print n; ":",
Print fac1(n),
Print fac2(n)
Next n
End Sub
Function fac1(n As Integer) As Double
Dim x As Double
x = 1
For i = 2 To n
x = x * i
Next i
fac1 = x
End Function
Function fac2(n As Integer) As Double
If n < 2 Then
fac2 = 1
Else
fac2 = fac2(n - 1) * n
End If
End Function
#include<iostream>
using namespace std ;
long factorial(int n) ;
void main(void)
{ for(i=0;i<=5;i++)
cout << " 阶乘 " << factorial(i) << endl << " " ;
return ;
}
long factorial(int n)
{
return n == 1 ? 1 : n * factorial(n-1) ;
}
我会编,就是没见着分啊
用VB写的~
Private Sub Form_Load()
Dim fn&, f1&, f2&, n%
f1 = 1: f2 = 1
n = Val(InputBox("请输入n"))
fn = f1 + f2
Print f1
Print f2
Print fn
For i = 1 To n
f1 = f2
f2 = fn
fn = f1 + f2
Print fn
Next i
End Sub
double f(int x,int n){
if(n==1) return x;
else return -(n-1)*pow(x,n)*f(x,n-1);
}
记得头文件添加 #include "math.h"
#include<iostream>
using namespace std;
char * delchar(char *s1, char s2) 函数定义
{
if (*s1 == '\0')
return s1;
if (*s1 == s2) {
strcpy(s1, s1 + 1);
delchar(s1, s2);
} else {
delchar(++s1, s2);
}
return s1;
}
int main()
{
char s1[] = "bsklambdabag";
char s2 = 'a';
cout<<"原来的字符串是:\n"<<s1<<endl;
delchar(s1, s2); 函数调用
cout<<"删除后指定字符后的字符串是:\n"<<s1<<endl;
return 0;
}
#include <stdio.h>
int Func(int);
int Sum(int);
int main ()
{
int i,n;
printf("请输入n值:");
scanf("%d",&n);
printf("1!+2!+...+n!=%d\n",Sum(n));
return 0;
}
int Func(int n) 递归求n!
{
int sum=1;
if(n==1 || n==0)
return 1;
else
sum=n*Func(n-1);
return sum;
}
int Sum(int n) 求1!+2!+。。。+n!的结果
{
int i=1,sum=0;
for(;i<=n;i++)
{
sum+=Func(i);
}
return sum;
}
二分查找前提数组元素已按升序排序
int binsearch(int value, int a[], int nArrayLen)
{
int nMid = nArrayLen / 2;
if (value == a[nMid])
return nMid; 找到下标
if (nMid == 0)
return -1; 找不到
if (value < a[nMid])
return binsearch(value, a, nMid);
else
return binsearch(value, a + nMid, nArrayLen - nMid);
}
int lianjia(int a)
{
if (a>1)
return (lianjia(a-1)+a);
else return 1;
}
int main()
{
int a=lianjia(100);
printf("%d",a);
return 0;
}