C语言第八周作业(局部变量,全局变量,递归)

1.
int a;

void f(int b)
{
	int c;
}
void g(void)
{
	int d;
	{
		int e;
	}
}
int main(void)
{
	int f;
}

(a)f函数
变量名字 :c
形参名字 :b
(b)g函数
变量名字 :d、e
(c) 声明e的程序块
变量名字 :e
(d) main函数
变量名字 :c
形参名字 :b

2.
2.

(a) extern主要用于表示能被几个文件共享的变量或函数
(b) static这种存储类型,可以实现这种“信息隐藏”技术。
(c) static和extern都会影响变量的存储期限,具体来说是把自动存储期限变为静态存储期限。

3.
extern float a;
void f(register double b)
{
	static int c;
	auto char d;

extern float a : 静态存储期限、文件作用域、外部链接
register double b :自动存储期限、块作用域、无链接
static int c :静态存储期限、块作用域、无链接
auto char d :自动存储期限、块作用域、无链接

5.

(a)错误。声明的变量如果具有static存储类型,当它在块内时,显然具有静态存储期限,但是它具有块作用域
(b)错误。在函数内部声明的变量,如果具有extern存储类型,是具有外部链接的。
(c)正确。具有内部链接的变量具有static存储类型,显然具有静态存储期限。
(d)正确。虽然函数的形式参数声明在函数的花括号之前,但他们也具有块作用域,属于函数体这个块。

15.

(a)存储期限

3.
1.

(递归)

#include <stdio.h>
#include <time.h>

int fib(int n);

int main(){
    int n;
    int tm;

    printf("enter a number:");
    scanf("%d",&n);
    
    tm = time(NULL);

    printf("%d\n",fib(n-1));

    tm =time(NULL) -tm;
    printf("the time is %d",tm);

    return 0;
}
int fib(int n){
    if (n<2){
        return n;
    } else{
        return fib(n-1)+fib(n-2);
    }
}

在这里插入图片描述
(循环)

#include <stdio.h>
#include <time.h>

int main(){
    int n,i=1;
    int tm;

    printf("enter a number:");
    scanf("%d",&n);

    tm = time(NULL);

    int a1=0,a2=1;
    while (i<n){
        int t=a1;
        a1=a2;
        a2=t+a2;
        i++;
    }
    printf("%d\n",a1);

    tm = time(NULL)-tm;
    printf("the time is %d",tm);

    return 0;
}

在这里插入图片描述
同一个问题用同一个数字45,递归实现有时候3秒有时候4秒,循环实现几乎不费时间。课间循环运算有效率一些,因为递归进行了很多次重复运算。

3.
(a)
#include <stdio.h>

int shulie(int,int,int);

int main(){
    int a,d,n;
    printf("enter three numbers:");
    scanf("%d %d %d",&a,&d,&n);
    printf("%d",shulie(a,d,n));

    return 0;
}
int shulie(int a,int d,int n){
    if (n==1){
        return a;
    }else{
        return shulie(a,d,n-1)+d;
    }
}

在这里插入图片描述

(b)
#include <stdio.h>

int shulie(int,int,int);

int main(){
    int a,d,n;
    printf("enter three numbers:");
    scanf("%d %d %d",&a,&d,&n);
    int sum=0;
    for(int i=1;i<=n;i++){
        sum+=shulie(a,d,i);
        //printf("%d",sum);
    }
    printf("%d",sum);

    return 0;
}
int shulie(int a,int d,int n){
    if (n==1){
        return a;
    }else{
        return shulie(a,d,n-1)+d;
    }
}

在这里插入图片描述

4.
#include <stdio.h>

int gcd(int, int);

int main(){
    int a,b;
    printf("Enter two integers:");
    scanf("%d %d",&a,&b);
    
    printf("Greatest common divisor:%d",gcd(a,b));

    return 0;
}
int gcd(int a,int b){
    int t=a;
    a=b;
    b=t%b;
    if(b==0){
        return a;
    }else{
        return gcd(a,b);
    }
}

在这里插入图片描述

5.
#include <stdio.h>

int exchange(int);

int main(){
    int n;
    printf("enter a number:");
    scanf("%d",&n);

    exchange(n);    
    return 0;
}
int exchange(int n){
    int t = n%10;
    if (t>0){
        printf("%d",t);
    }
    return exchange(n/10);
}

在这里插入图片描述

6.
a(递归)
#include <stdio.h>
#include <math.h>

int huiwen(int);
int power(int, int);

int flag=1;

int main(){
    int n;
    printf("enter a number:");
    scanf("%d",&n);
    huiwen(n);
    
    return 0;
}

int huiwen(int n){
    int cnt=0;
    int n1=n;
    do{ 
        int t=n1%10;
        cnt +=1;
        n1 /=10;
    }while(n1>0);

    int flag=1;
    int i=1;
    do{    
        int len=n/power(10,cnt-i);
        int t=n%10;
        if(len!=t){
            printf("No!");
            break;
        }else{
            n %= power(10,cnt-i);
            n /=10;
            i++;
            if(flag){
                printf("Yes!");
                break;
            }
            return huiwen(n);
        }
    }while(i<cnt-1-i);
}
int power(int x,int n){
    if (n==0){
        return 1;
    }else{
        return x*power(x,n-1);
    }
}
b(循环)
#include <stdio.h>

int main(){
    int n;
    char digit[10];
    printf("enter a number:");
    scanf("%d",&n);
    
    int cnt=0;
    do{
        int t=n%10;
        digit[cnt]=t;

        cnt +=1;
        n /=10;
    }while(n>0);
    /*
    printf("%d\n",cnt);
    for(int i=0;i<cnt;i++){
        printf("%d\n",digit[i]);
    }*/

    int flag=1;
    int i=0;
    do{
        if(digit[i]!=digit[cnt-1-i]){
            flag=0;
            printf("No!");
            break;
        }
        i++;
    }while(i<cnt/2-1);
    if (flag){
        printf("Yes!");
    }
    return 0;
}

在这里插入图片描述

7.
#include <stdio.h>

int power();

int main(){
    int x,n;
    printf("enter two numbers:");
    scanf("%d %d",&x,&n);

    printf("%d",power(x,n));

    return 0;
}
int power(int x,int n){
    if (n==0){
        return 1;
    }else{
        if (n%2==0){
            return power(x,n/2)*power(x,n/2);
        } else{
            return x*power(x,n-1);
        }
    }
}

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cachel wood

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值