C语言必会100题(6)。利用递归方法求5!/利用递归函数调用方式,反向打印字符/五个人问岁数/输入一个不多于5位数的整数,判断是几位数/输入一个5位数,判断它是不是回文数

特此鸣谢:鱼C_小甲鱼(B站up主)不二如是(鱼C论坛大佬)
题目来源:https://fishc.com.cn
注:这些题在网上都可以搜到,题下面的代码大多是流传下来的答案(我重新排了一下版,增加了可读性),部分是本人经过深思熟虑后编写的。

26,利用递归方法求5!

  • 题目:利用递归方法求5!。
  • 提示:采用函数形式,在函数里面可以递归,调用自己。
  • 程序源代码:
    #include <stdio.h>
    
    int fact(int j);
    
    int main()
    {
        int i;
    
        for(i = 1; i < 6; i++)
            printf("%d! = %d\n", i, fact(i));
        return 0;
    }
    
    int fact(int j)
    {
        int sum;
        if(j == 0)
            sum = 1;
        else 
            sum = j * fact(j - 1);
        return sum;
    }

27,利用递归函数调用方式,反向打印字符

  • 题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
  • 程序源代码:
    #include <stdio.h>
    
    void palin(int n);
    
    int main()
    {
        int i = 5;
    
        printf("\40:"); // \40在屏幕上显示空格,转义字符八进制,转为十进制为32,ASCII码表中表示空格
        palin(i);
        printf("\n");
        return 0;
    }
    
    void palin(int n)
    {
        char next;
        if(n <= 1)
        {
            next = getchar(); // 读取字符
            printf("\n:");
            putchar(next); // 输出字符
        }
        else
        {
            next = getchar();
            palin(n - 1);
            putchar(next);
        }
    }

28,五个人问岁数

  • 题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后 问第一个人,他说是10岁。请问第五个人多大?
  • 程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。
  • 程序源代码:
    #include <stdio.h>
    
    int age(int n);
    
    int main()
    {
        printf("%d\n", age(5));
        return 0;
    }
    
    int age(int n)
    {
        int c;
    
        if(n == 1) {
            c = 10;
        } else {
            c = age(n - 1) + 2;
        }
    
        return c;
    }

29,输入一个不多于5位数的整数,判断是几位数

  • 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
  • 程序源代码:
  1. 第一种解:
    #include <stdio.h>
    
    void reversal();
    
    int main()
    {
        int count = 0;
        int a[10];
        int n;
        int i = -1;
    
        printf("请在屏幕上输入不多于5位的数字:\n");
    
        // 使用getchar()函数会有bug,但是我是一个初学者,所以凑合着用吧
        // getchar()函数只接收字符,会将其转化为ASCII码值
        while ( (n = getchar()) != '\n' ) {
            i++;
            a[i] = n;
            count++;
        }
    
        printf("总共有%d位数\n", count);
        printf("颠倒后的数字为:");
        for (int j = count; j >= 0; j--) {
    
            putchar(a[j]);
            // 不能这么做。
            // printf("%d\n", a[j]);
        }
    
        return 0;
    }
  2. 第二种解:
    // 这个用的是递归,但是我不会计数器来判断用户输入的数字个数
    // 有大佬路过的话,麻烦帮忙看一下
    #include <stdio.h>
    
    void reversal();
    
    int main()
    {
        int i = 5;
    
        printf("请在屏幕上输入不多于5位的数字:\n");
    
        reversal(i);
    
        return 0;
    }
    
    
    void reversal(int n) {
    
        int next;
    
        if(n <= 1)
        {
            next = getchar(); // 读取字符
            putchar(next); // 输出字符
        }
        else
        {
            next = getchar();
            reversal(n - 1);
            putchar(next);
        }
    }
  3. 第三种解:
    #include <stdio.h>
    
    int main( )
    {
        long a, b, c, d, e, x;
    
        printf("请输入不多于5位的数字:\n");
        scanf("%ld", &x);
    
        a = x / 10000; // 分解出万位
        b = x % 10000 / 1000; // 分解出千位
        c = x % 1000 / 100; // 分解出百位
        d = x % 100 / 10; // 分解出十位
        e = x % 10; // 分解出个位
    
        if (a != 0) {
            printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a);
        } else if (b!=0) {
            printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b);
        } else if (c!=0) {
            printf(" there are 3,%ld %ld %ld\n",e,d,c);
        } else if (d!=0) {
            printf("there are 2, %ld %ld\n",e,d);
        } else if (e!=0) {
            printf(" there are 1,%ld\n",e);
        }
    
        return 0;
    }

30,输入一个5位数,判断它是不是回文数

  • 题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
  • 程序源代码:
  1. 第一种解:
    #include <stdio.h>
    
    int main() {
    
        int a;
        int array[5];
        int num = -1;
    
        printf("请输入一个5位数:");
    
        while ( (a = getchar()) != '\n') {
            num++;
            array[num] = a;
        }
    
        if (array[0] == array[4] && array[1] == array[3]) {
            printf("这是一个回文数!");
        } else {
            printf("这不是一个回文数!");
        }
        
        return 0;
    }
  2. 第二种解:
    #include <stdio.h>
    
    int main( )
    {
        // 个位数:units
        // 十位数:tens
        // 千位数:thousands
        // 万位数:ten_thousand
        long units, tens, thousands, ten_thousand, x;
    
        printf("请输入一个5位数:");
        scanf("%ld", &x);
    
        ten_thousand = x / 10000;
        thousands = x % 10000 / 1000;
        tens = x % 100 / 10;
        units = x % 10;
    
        // 个位等于万位并且十位等于千位
        if(units == ten_thousand && tens == thousands) {
            printf("this number is a huiwen\n");
        } else {
            printf("this number is not a huiwen\n");
        }
    
        return 0;
    }
    

     

posted @ 2022-03-20 17:41  炸天帮帮主  阅读(290)  评论(0)    收藏  举报