2024.02.11作业

本文介绍了如何使用C语言通过递归实现几个常见的数学运算(如阶乘、和、斐波那契数列)、字符串操作(二进制转换、单词逆置)以及数字处理(位数字之和与乘积)。

1.请使用递归实现n!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int func(int n)
{
    if (n == 1)
    {
        return 1;
    }

    return func(n - 1) * n;
}

int main()
{
    int n = 5;

    printf("%d\n", func(5));

    return 0;
}


2.请使用递归实现0-n的和

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int func(int n)
{
    if (n == 1)
    {
        return 1;
    }

    return func(n - 1) + n;
}

int main()
{
    int n = 5;

    printf("%d\n", func(5));

    return 0;
}


3.请使用递归实现斐波那契

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int func(int n)
{
    if (n <= 0)
    {
        return 0;
    }

    if (n == 1 || n == 2)
    {
        return 1;
    }

    int a = func(n - 1) + func(n - 2);

    return a; 
}

int main()
{
    int n = 10;

    for (int i = 1; i < n; i++)
    {
        printf("%4d", func(i));
    }
    puts("");

    return 0;
}


4.请使用递归实现二进制转换

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void func(int n)
{
    if (n < 2)
    {
        printf("%d", n);
        return;
    }

    func(n / 2);
    printf("%d", n % 2);
}

int main()
{
    int n;
    scanf("%d", &n);

    func(n);
    puts("");

    return 0;
}


5.请递归实现计算各个位数字之和

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int func(int n)
{
    if (n == 0)
    {
        return 0;
    }

    return n % 10 + func(n / 10);
}

int main()
{
    int n;
    scanf("%d", &n);

    printf("%d\n", func(n));

    return 0;
}


6.请递归实现计算各个位数字的乘积

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int func(int n)
{
    if (n == 0)
    {
        return 1;
    }

    return n % 10 * func(n / 10);
}

int main()
{
    int n;
    scanf("%d", &n);

    printf("%d\n", func(n));

    return 0;
}


7.请使用递归实现计算单词逆置

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void func(char* s, int i, int j)
{
    if (i >= j)
    {
        return;
    }

    char t = s[i];
    s[i] = s[j];
    s[j] = t;

    func(s, i + 1, j - 1);
}

int main()
{
    char s[] = "hello";
    int len = strlen(s) - 1;

    func(s, 0, len);
    puts(s);

    return 0;
}

import { createApp } from 'vue' import App from './App.vue' import router from './router' import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' import * as ElementPlusIconsVue from '@element-plus/icons-vue' // 初始化本地存储数据(仅首次加载时执行) const initLocalData = () => { // 1. 用户数据(教师+学生) if (!localStorage.getItem('users')) { const users = [ { id: 1, username: 'teacher01', password: '123456', name: '张老师', role: 'teacher' }, { id: 2, username: 'student01', password: '123456', name: '李同学', role: 'student' }, { id: 3, username: 'student02', password: '123456', name: '王同学', role: 'student' } ] localStorage.setItem('users', JSON.stringify(users)) } // 2. 班级数据 if (!localStorage.getItem('classes')) { const classes = [ { id: 1, className: '计算机2101班', teacherId: 1 }, { id: 2, className: '计算机2102班', teacherId: 1 } ] localStorage.setItem('classes', JSON.stringify(classes)) } // 3. 作业数据 if (!localStorage.getItem('homeworks')) { const homeworks = [ { id: 1, homeworkName: 'Vue 期末作业', classId: 1, className: '计算机2101班', deadline: '2024-12-31 23:59:59', description: '完成一个 Vue 3 小项目,包含路由和组件', teacherId: 1, createTime: '2024-11-01 10:00:00' } ] localStorage.setItem('homeworks', JSON.stringify(homeworks)) } // 4. 作业提交数据 if (!localStorage.getItem('submissions')) { const submissions = [ { submitId: 1, homeworkId: 1, studentId: 2, submitTime: '2024-11-05 14:30:00', filename: 'Vue期末作业_李同学.zip' } ] localStorage.setItem('submissions', JSON.stringify(submissions)) } } initLocalData() const app = createApp(App) // 全局注册Element Plus图标 for (const [key, component] of Object.entries(ElementPlusIconsVue)) { app.component(key, component) } app.use(router).use(ElementPlus) app.mount('#app')
12-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值