zoukankan      html  css  js  c++  java
  • 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 = 1; i <= n; i++)
        {
            res = res*i;
        }
        return res;
    }
    
    
    void main(){
        //方法2
        float n, s = 0, t = 1;
        for (n = 1; n <= 20; 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
    ", 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");
    }

    //题目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==1)
        {
            return 1;
        }
        else{
            return run8(n - 1)*n;
        }
    }
    
    void main(){
        //方法1
        printf("5!===%f", run8(5));
        system("pause");
    }

    //题目27:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
    
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    //分析:把字符放入数组中,逆序,就是调用本身放在输出前面就OK了
    char str[6] = "abcde";
    
    void run1(int n){
        if (n==0)
        {
            printf("%c",str[n]);
        }
        else{
            printf("%c", str[n]);
            run1(n-1);
            
        }
    }
    
    void main(){
        char *p = str;
        //char *p = "sdfasd";//错误 "sdfasd"字符串指针是个常量,不可以修改,能修改的只有字符串数组
        scanf("%s", p);//正确
        //scanf("%s", str);//正确
    
        run1(4);
        system("pause");
    }

  • 相关阅读:
    OleView.exe:查看机器上的COM 组件。
    COM中导出GUID
    进程外组件以及进程间通信方式
    拼接多个 wchar_t *
    wstring to wchar_t*
    BSTR
    GetProcAddress 使用注意事项
    C++和.net的集合类对应
    COM的一些基本概念
    Error Lookup工具
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/5151646.html
Copyright © 2011-2022 走看看