Lab07-Recursion and Function(2019.11.11)

Lab07-Recursion and Function

1. 正整数的各位数字之和【简单】

(递归)64.请编写递归程序,计算给定正整数的各位数字之和。例如,如果实参为12345,则函数返回1+2+3+4+5=15,即返回15.

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

int sum(int num)
{
    if(num<10)
        return num;
    return (num%10 + sum(num/10));
}

int main()
{
    int num;
    scanf("%d",&num);
    printf("%d",sum(num));
    return 0;
}

2. 逆序输出【简单】

(递归)65.请编写递归函数,该函数每次调用时将会让用户输入一个正整数,如果输入0或者负数时程序结束。函数将按照逆序输出整数值。
例如用户输入 10 20 30 0 程序输出 30 20 10

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

void daoxu(int x)
{
    if(x<=0)
        return;
    int i;
    scanf("%d",&i);
    daoxu(i);
    printf("%d ",x);
        }

int main()
{
    int n;
    scanf("%d",&n);
    daoxu(n);
    return 0;
}

3. 十进制转二进制【简单】

(递归)66.编写递归程序将十进制整数n转换成二进制数,要求从高位到低位输出二进制数的结果。

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

void f(int n)
{
if(n) 
    f(n/2);   //先计算后面的结果,遇零返回后由于后面先算,就倒着输出了
else return;
printf("%d",n%2);
}

int main()
{
int n;
scanf("%d",&n);
if(n==0) printf("0");
f(n);
printf("\n");
return 0;
}
/*以12为例
1、12
void f(int n)
{
if(n!=0) //12不为0
{
f(n/2); //先输出12/2=6的结果
printf("%d",n%2); //12%2=0   输出0
}
}
2、6
void f(int n)
{
if(n!=0) //6不为0
{
f(n/2); //先输出6/2=3的结果
printf("%d",n%2); //6%2=0   输出0
}
}
3、3
void f(int n)
{
if(n!=0) //3不为0
{
f(n/2); //先输出3/2=1的结果
printf("%d",n%2); //3%2=1   输出0
}
}
4、1
void f(int n)
{
if(n!=0) //1不为0
{
f(n/2); //先输出1/2=0的结果   遇0返回
printf("%d",n%2); //1%2=1   输出1
}
}
5、0 遇0返回
从后向前输出,结果为1100  */

4. 最大公约数【简单】

(递归)67.请编写递归程序求解两个数的最大公约数;int GDB(int a, int b)
GDB(a.b) = a if b =0
GDB(a.b)=GDB(b, a%b), if b >0

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

int GDB(int a, int b)
{
    if(b==0)
        return a;
    if(a%b==0)
        return b;
        return GDB(b,a%b);
}

int main()
{
    int a,b;
    scanf("%d %d",&a,&b);
    printf("%d",GDB(a,b));

    return 0;
}

5. fibonacci函数【简单】

(递归)68.请用递归的形式实现fibonacci函数.
int fibonacci(int n)
/* fibonacci: recursive version
Pre: The parameter n is a nonnegative integer.
Post: The function returns the nth Fibonacci number.

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

int fibonacci(int n)
{
    if(n==0)
        return 0;
    if(n==1)
        return 1;
    return fibonacci(n-1)+fibonacci(n-2);
}

int main()
{
    int n;
    scanf("%d",&n);
    printf("%d",fibonacci(n));
    return 0;
}

6. *号构成的菱形图案【中等】

(循环)
输入一个正整数repeat (0<repeat<10),做repeat次下列操作:
输入一个正整数n,打印一个边长为 n 的菱形图案,注意,每一行最后一个后面要求输出一个空格!
输出使用以下语句:
printf(" “);
printf(”
“);
printf(”\n");
输入输出示例:括号内为说明
输入
2 (repeat=2)
3 (n=3)
4 (n=4)
输出
在这里插入图片描述

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

int main()
{
    int repeat,i,j,k,n;
    scanf("%d",&repeat);
    for(i=1; i<=repeat; i++){
        scanf("%d",&n);
        for(k=1;k<=n;k++){
            for(j=1;j<=2*(n-k);j++)
                printf(" ");
            for(j=1;j<=2*k-1;j++)
                printf("* ");
            printf("\n");
        }
        for(k=n-1;k>=1;k--){
            for(j=1;j<=2*(n-k);j++)
                printf(" ");
            for(j=1;j<=2*k-1;j++)
                printf("* ");
            printf("\n");
        }
    }
    return 0;
}

7. 函数调用中的平均值【中等】

60.(函数)Write a value-returning float function called RunningAvg that takes a float variable, value, as its input and returns the running average of all the values that have been passed to the function since the program first call it. For example RunningAvg(10); //return 10 RunningAvg(20);//return 15 RunningAvg(30);//return 20
输入为-1时结束。每行输出对应每行输入,输出保留2位小数。
样例输入:
10
20
30
-1
样例输出:
10.00
15.00
20.00

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

int main()
{
    float a,average,i=1,sum=0;
    scanf("%f",&a);
    while(a!=-1)
    {
        sum = sum+a;
        average = sum/i;
        printf("%.2f\n",average);
        i++;
        scanf("%f",&a);
    }
    return 0;
}

8. 日期间相隔的天数【困难】

39.用户输入包含年,月,日的日期,请计算两个日期间相隔的天数;
样例输入1:
2015 10 30 2016 4 15
样例输出1:
采集时间为168天
样例输入2:
2011 6 1 2016 11 10
样例输出:
采集时间为1989天

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

int year0 = 1000;
int _ydays(int year);
int _mdays(int year, int month, int days);

int main()
{
    int year1, year2, y_total_days1, y_total_days2, m_total_days1, m_total_days2, total_days;
    int month1, month2, day1, day2;
    scanf("%d %d %d %d %d %d",&year1,&month1,&day1,&year2,&month2,&day2);

    y_total_days1 = _ydays(year1);
    y_total_days2 = _ydays(year2);

    m_total_days1 = _mdays(year1,month1,day1);
    m_total_days2 = _mdays(year2,month2,day2);

    total_days =  y_total_days2+m_total_days2-(y_total_days1+m_total_days1);

    printf("采集时间为%d天",total_days);

    return 0;
}


int _ydays(int year)//计算该年1月1日到1970年1月1日的所有天数
{
    year--;
    int days,_years;
    int ly, xly;
    _years = year-year0;
    for(ly = 1; year>year0; year--){
        if((0==year%4 && 0!=year%100) || 0==year%400)
           ly++;
    }
    xly = _years-ly;
    days = ly*366+xly*365;
    return days;
}

int _mdays(int year, int month, int days)//计算该天到1月1日的天数
{
    int m_total_days=0;
    int i,mdays;
    if((0==year%4 && 0!=year%100) || 0==year%400){
       for(i=month-1; i>=1; i--){
            switch(i)
            {
                case 1:case 3:case 5:case 7:case 8:case 10:case 12: mdays = 31; break;
                case 4:case 6:case 9:case 11: mdays = 30;break;
                case 2: mdays = 29;break;
				default:break;
            }
            m_total_days += mdays;
        }
        m_total_days +=  days;
    }
    else
    {
        for(i=month-1; i>=1; i--){
            switch(i)
            {
                case 1:case 3:case 5:case 7:case 8:case 10:case 12: mdays = 31;break;
                case 4:case 6:case 9:case 11: mdays = 30;break;
                case 2: mdays = 28;break;
				default:break;
            }
            m_total_days += mdays;
        }
        m_total_days +=  days;
    }
    return m_total_days;
}

9. 取模【中等】

(循环)求n的阶乘mod 20181111的结果。输入n范围是(0<=n<=100),输出n!mod 20181111之后的数字。(即对20181111取余数)
样例输入:
5
样例输出:
120

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


int main()
{
    int i,num;
    long long total,ch1;
    scanf("%d",&num);
    total = num;
    if(num == 0)
        total = 1;
    else{
        for(i=num-1; i>=1; i--){
            total = (total%20181111) * (i%20181111);
        }
    }
    ch1 = total%20181111;
    printf("%lld",ch1);
    return 0;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值