C语言 百炼成钢9

//题目25:求1+2!+3!+...+20!的和

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h> //分析:这题用函数很好解决,表面看起来很复杂,但是我们写一个函数来求n!(n的阶乘)
//然后写一个for循环不断调用函数就OK了 float run9(int n){
float res = 1.0;
for (int i = ; i <= n; i++)
{
res = res*i;
}
return res;
} void main(){
//方法2
float n, s = , t = ;
for (n = ; n <= ; n++)
{
//n的阶乘 就是(n-1)*n
t *= n;//1*1+1*2+2*3+6(2*3)*4
s += t;
}
printf("1+2!+3!...+20!=%f\n", s);
//方法1
/*float sun = 0.0;
for (int i = 1; i <= 20; i++)
{
sun = sun + run9(i);
}
printf("求1+2!+3!+...+20!的和%f", sun);*/
system("pause");
}

C语言 百炼成钢9

//题目26:利用递归方法求5!。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h> //分析:如题递归,n!=(n-1)!*n float run8(int n){
if (n==)
{
return ;
}
else{
return run8(n - )*n;
}
} void main(){
//方法1
printf("5!===%f", run8());
system("pause");
}

C语言 百炼成钢9

//题目27:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h> //分析:把字符放入数组中,逆序,就是调用本身放在输出前面就OK了
char str[] = "abcde"; void run1(int n){
if (n==)
{
printf("%c",str[n]);
}
else{
printf("%c", str[n]);
run1(n-); }
} void main(){
char *p = str;
//char *p = "sdfasd";//错误 "sdfasd"字符串指针是个常量,不可以修改,能修改的只有字符串数组
scanf("%s", p);//正确
//scanf("%s", str);//正确 run1();
system("pause");
}

C语言 百炼成钢9

上一篇:mysql插入一万条数据


下一篇:Codeforces Round #197 (Div. 2) C,D两题