9.11作业

题目1:以下程序的正确运行结果是( )。(鲁科安全)

int f(int a);

int main(void)

{

    int a = 2,i;

for(i = 0; i < 3; i++)

printf("%4d", f(a));

}

int f(int a)

{

    int b = 0;

    static int c = 3;

b++;

c++;

    return (a+b+c);

}

A. 777                 B. 7 10 13                C. 7 9 11          D. 7 8 9

答案:D

题目2:在一个被调用函数中,关于return语句使用的描述,( )是错误的 (软通动力)

A. 被调用函数中可以不用return语句

B. 被调用函数中可以使用多个return语句

C. 被调用函数中,如果有返回值,就一定要有return语句

D. 被调用函数中,一个return语句可以返回多个值给调用函数

答案:D

题目3:以下程序的运行结果为( ) (鲁科安全)

#include <stdio.h>

#include <string.h>

int SubCount(char *dest, int count)

{

    strcpy(dest, "555");

    count++;

    return 0;

}

int main()

{    int count = 3;

    char caBuf[8];

    SubCount(caBuf, count);

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

    return 0;

}

A. 8             B. 4                  C. 3                  D. 5

答案:C

main函数中定义了count=3

题目4:请问运行Test函数会有什么样的结果?(华辰泰尔)

char *GetMemory(void)

{

    char p[] = "hello world";

    return p;

}

void Test(void)

{

    char *str = NULL;

    str = GetMemory();

    printf(str);

}

答案:hello world

题目5:分析以下程序,写出运行结果并写出过程 (广域科技)

#include <stdio.h>

#include <stdlib.h>

void getalloc(char *p)

{

    p = (char *)malloc(100);

    strcpy(p, "hello world");

}

int main()

{

    char *str = NULL;

    getalloc(str);

    printf("%s\n",str);

    free(str);

    return 0;

}

答案:hello world

p在副本里面,p的修改不会影响主函数

题目6:下列程序的输出结果是________。(富士安全)

fun(int a, int b, int c)

{

    c = a*b;

}

void main()

{

    int c = 10;

    fun(2,3,++c);

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

}

答案:11

main()主函数中定义c=10进行++

题目7:找错题,说明那里错了(恩易物联1,深圳元征信息科技)

void test1()

{

    char string[10];

    char *str1 = "0123456789";

    strcpy( string, str1 );

}

 答案:定义的字节长度只有10,把10改成11

0123456789后还有一个‘\0’

题目8:下面的代码片段会输出__________ (飞音时代)

void test(void)

{

    char *p = NULL;

    strcpy(p, "hello");

    printf("%s", p);

}

答案:空指针不能进行函数操作

题目9:sizeof(str); 的值是多少? (21年中航安为)

void Func(char str[100])

{

sizeof(str);

}

答案:101

题目10:递归函数最终会结束,那么这个函数一定( );(北京信果科技)

A. 使用了局部变量

B. 有一个分支不调用自身

C. 使用了全局变量或者使用了一个或多个参数

答案:B

题目11:程序如下,程序执行后的输出结果是: (中科四平)

int f(int x, int y)

{

    return (y-x)*x;

}

void main()

{

    int a = 3,b=4,c=5,d;

    d=f(f(3,4),f(3,5));

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

}

答案:9 d=(-6-(-3))*3

题目12:请判断下面程序输出的值是多少? (信雅达)

int func(int a)

{

    static int b = 0;

    b+=a;

    return b;

}

int main(void)

{

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

}

答案:1 4 9

题目13:这段程序的输出是(________) (青岛汉泰)

void f1(int *, int);

void f2(int *, int);

void(*p[2]) (int *, int);  

main()

{

    int a;

    int b;

    p[0] = f1;

    p[1] = f2;

      a=3;

      b=5;

      p[0](&a, b);

    printf("%d\t %d\t", a, b);

    p[1](&a, b);

    printf("%d\t %d\t", a, b);

}

void f1(int * p, int q)

{

    int tmp;

    tmp = *p;

    *p = q;

    q = tmp;

}

void f2(int *p, int q)

{

    int tmp;

    tmp = *p;

    *p = q;

    q = tmp;

}

A. 5 5 5 5            B. 3 5 3 5                 C. 5 3 5 3                 D. 3 3 3 3

 答案:A

题目14:有以下程序段, x=7执行后的值为 ( ) (杭州快越科技)

int fun(int x) {

int p;

if(x==0||x==1)

return(3);

    p=x-fun(x-2);

return p;

}

A. 0             B. 2                  C. 5                  D. 6

答案:B

题目15:有以下函数,该函数的返回值是:( ) (山东信通电子)

char *fun(char *p)

{

    return p;

}

A. 无确切的值                                   B. 形参 p 中存放的地址值

C. 一个临时存储单元的地址          D. 形参 p 自身的地址值

答案:B

题目16:编写strcpy函数 (山东山大电力技有限公司)

已知strcpy 函数的原型是

char *strcpy(char *strDest,const char *strSrc);其中 strDest 是目的字符串,strSrc 是源字符串。

(1)、不调用 C 的字符串库函数,请编写函数 strcpy。

(2)、strcpy 能把 strSr 的内容复制到 strDest,为什么还有 char"类型的返回值?

(3)、strcpy 和 memcpy 的区别。

答案:

#include <stdio.h>  
char *my_strcpy(char *strDest, const char *strSrc) 
{   
    if (strDest == NULL) {  
        return NULL;  
    }  
    while ((*strDest++ = *strSrc++) != '\0') {  
    }  

    return strDest - 1; 
}    
int main() {  
    char src[] = "ynbb";
    my_strcpy(dest, src);    
    printf("Copied string: %s\n", dest);  
    return 0;  
}

题目17:请实现一个函数,输入一个整数,输出该数二进制表示中的1的个数。例如:把9表示成二进制是1001,有2位是1。因此如果输入9,该函数输出2。(矩阵软件)

答案:

#include <stdio.h>
 
#include <string.h>
 
#include <stdlib.h>
 
int cout(int x){
 
        if(x==1){
 
          return 1;
 
        }else{
 
          if(x%2==1){
 
                   x=x/2;
 
                   return cout(x)+1;
 
          }  else {
 
                   x=x/2;
 
                   return cout(x);
          }
 
        }
 
}
 
int main(int argc, const char *argv[])
 
{
 
        int n;
 
        scanf("%d",&n);
 
        printf("%d\n",cout(n));
 
        return 0;
 
}

题目18: 请用编写递归算法计算fibinacci数列第1000位的值。斐波拉契数列为1,1,2,3,5,8,13,21,…… (北京凝思软件)

 

答案:

#include <stdio.h>
 
#include <string.h>
 
#include <stdlib.h>
 
long int fb(int x){
 
        if(x==1||x==2){
 
          return 1;
 
        }else{
 
          return fb(x-1)+fb(x-2);
 
        }
 
}
 
int main(int argc, const char *argv[])
 
{
 
        int x=1000;
 
        printf("%ld\n",fb(x));
 
        return 0;
 
}

题目19:用 C 语言写一个递归算法求 N!(华辰泰尔)

答案:

#include <stdio.h>
 
#include <string.h>
 
#include <stdlib.h>
 
int jc(int x){
 
         if(x==1) return 1;
 
         else return x*jc(x-1);
 
}
 
int main(int argc, const char *argv[])
 
{
 
         int n;
 
         scanf("%d",&n);
 
         printf("%d的阶乘是:%d\n",n,jc(n));
 
         return 0;
 
}

  • 11
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值