zoukankan      html  css  js  c++  java
  • 浙江大华2012笔试题+答案解析

    请写出下面程序的输出结果:

    1、

    1. int count = 3; 
    2. int main(void
    3.     int i, sum, count = 2; 
    4.     for(i=0,sum=0; i<count; i+=2,count++) 
    5.     { 
    6.         static int count = 4; 
    7.         count++; 
    8.         if(i%2 == 0) 
    9.         { 
    10.             extern int count; 
    11.             count++; 
    12.             sum += count; 
    13.         } 
    14.         sum += count; 
    15.     } 
    16.     printf("%d %d\n",count, sum); 
    17.     return 0; 

    2、

    1. void func(char str[50]) 
    2.     printf("A %d B %d ",sizeof(str), strlen(str)); 
    3. int main(void
    4.     char stra[] = "HelloWorld"; 
    5.     char *strb = stra; 
    6.     printf("C %d D %d ",sizeof(stra), sizeof(strb++)); 
    7.     func(++strb); 
    8.     printf("E %d F %d\n",strlen(stra), strlen(strb++)); 
    9.     return 0; 

    3、

    1. #include <vector> 
    2. int func(std::vector<int>vec) 
    3.     static int k = 2; 
    4.     std::vector<int>::reverse_iterator it; 
    5.     for(it = vec.rbegin(); it!=vec.rend(); ++it) 
    6.     { 
    7.         k += *it%2==0? ++*it: (*it)++; 
    8.     } 
    9.     return k; 
    10. int main(void
    11.     std::vector<int>vec; 
    12.     for(int i = 0; i<4; i++) 
    13.     { 
    14.         vec.push_back(i); 
    15.         printf("%d ",func(vec)); 
    16.     } 
    17.     return 0; 

     4、

    1. class Base 
    2. public
    3.     int m_a; 
    4.     Base(int a=2):m_a(a) 
    5.     { 
    6.         printf("A %d ",m_a); 
    7.     } 
    8.     virtual ~Base() 
    9.     { 
    10.         printf("B %d ",m_a); 
    11.     } 
    12. }; 
    13. class Derived:public Base 
    14. public
    15.     Derived(int a=4):Base(a) 
    16.     { 
    17.         printf("C %d ",m_a); 
    18.     } 
    19.     ~Derived() 
    20.     { 
    21.         printf("D %d ",m_a); 
    22.     } 
    23. }; 
    24. int main(void
    25.     Base *aa,bb; 
    26.     aa = new Derived; 
    27.     delete aa; 
    28.     return 0; 

    5、

    1. class Base 
    2. public
    3.     int m_a,m_b; 
    4.     Base(int a = 2,int b = 5):m_a(a),m_b(b)  {  } 
    5.     int func_a() 
    6.     { 
    7.         return m_a - m_b; 
    8.     } 
    9.     virtual int func_b() 
    10.     { 
    11.         return m_a + m_b; 
    12.     } 
    13. }; 
    14. class Derived:public Base 
    15. public
    16.     Derived(int a = 4, int b = 7):Base(a, b)  {  } 
    17.     virtual int func_a() 
    18.     { 
    19.         return m_b + m_a; 
    20.     } 
    21.     int func_b() 
    22.     { 
    23.         return m_b - m_a; 
    24.     } 
    25. }; 
    26. int main(void
    27.     Base *aa, *bb; 
    28.     aa = new Base(4, 7); 
    29.     bb = new Derived(3, 5); 
    30.     printf("%d %d %d %d\n",aa->func_a(), aa->func_b(), bb->func_a(), bb->func_b()); 
    31.     delete aa; 
    32.     delete bb; 
    33.     return 0; 

    6、

    1. struct SC 
    2.     int a; 
    3.     int b; 
    4.     int c; 
    5. }; 
    6. struct SD 
    7.     int a; 
    8.     int b; 
    9.     int c; 
    10.     int d; 
    11. }; 
    12. int main(void
    13.     struct SC c1[] = {{3},{4},{5},{6}}; 
    14.     struct SD *c2 = (struct SD*)c1 + 1; 
    15.     printf("%d %d %d %d\n",c2->a,c2->b,c2->c,c2->d); 
    16.     return 0; 

    7、

    1. int func(int n) 
    2.     int k = 1; 
    3.     if(n > 0) 
    4.     { 
    5.         k += func(--n); 
    6.         printf("%d ", n); 
    7.         k += func(--n); 
    8.     } 
    9.     return k; 
    10.  
    11. int main(void
    12.     int a = 3; 
    13.     printf("%d\n",func(a)); 
    14.     return 0; 


    编程题:
    1、函数checkstr判断一字符串是不是对称的。其中msg为输入的字符串,对称返回0,不对称返回-1,实现该函数。
    int checkstr(const char *msg);

    2、给出一个单向链表的头指针,输出该链表中倒数第K个节点的指针,链表的倒数第0个节点为链表的尾节点(尾节点的next成员为NULL)
    typedef struct Node
    {
        struct Node *next;
    }NODE;

    NODE* findnode(NODE *head,unsigned int k);



    简答题:
    1、简述动态链接库DLL和静态链接库lib的差别。
    2、请简述MFC中的窗口收到WM_PAINT消息是如何处理的,什么情况下会产生WM_PAINT消息。
    3、请简述Critical Section 、Mutex、Semaphore的功能和差别
    4、简述多线程程序对比单线程程序的优点和缺点。

     

    参考答案(欢迎讨论)转载请注明来源http://www.cnblogs.com/jerry19880126/

    1. 4 20 主要考查各个count的作用范围,for循环判断条件的count是main下第一行的count,for循环里面的count是static的count,if语句里面的count是main外部的count,sum+=count中的count是static的count。
    2. C 11 D 4 A 4 B 9 E 10 F9 注意函数形参里的数组形式,其实本质上都是指针,另外sizeof是在编译阶段就处理的运算符,所以会忽略里面的各种算术运算。
    3. 3 5 10 18 考查运算符的优先级,带等号的运算符=、+=等等,优先级都是很低的,所以先做的是那个三目运算符,还要注意形参的变化不影响实参。
    4. A 2 A 4 C 4 D 4 B 4 B 2 构造时,先构造基类再构造派生类,释放时先释放派生类再释放基类。
    5. -3 11 -2 2 构成多态的条件是基类的virtual(注意是基类一定要有,派生类自动也会是virtual,但派生类的virtual不会传给基类),派生类对基类的覆盖(函数名、形参和返回值类型必须完全一样),基类指向派生类对象或引用。
    6. 0 0 5 0 注意地址转换后的+1,并不是地址值只偏移一个,而是偏移了一整个struct SD的空间,所以c1内容300400500600会一下子偏移掉4个int,指向了4后面的0。
    7. 递归,花些耐心推就行了。

    编程题

    1. 如下(引用自http://blog.csdn.net/Hackbuteer1):

    View Code
     1 int checkstr(const char *msg) 
     2 { 
     3     int len = strlen(msg); 
     4     int i, j; 
     5     for(i = 0,j = len-1; i <= j; i++,j--) 
     6     { 
     7         if(msg[i] != msg[j]) 
     8             break; 
     9     } 
    10     if(i>j) 
    11          return 0; 
    12     else 
    13         return -1; 
    14  }

    2. 如下:

    View Code
     1 typedef struct Node
     2 
     3 {
     4 
     5 struct Node *next;
     6 
     7 }NODE;
     8 
     9  
    10 
    11 NODE* findnode(NODE *head, unsigned int k)
    12 
    13 {
    14 
    15 unsigned int count = 0;
    16 
    17 NODE* pre = head;
    18 
    19 NODE* p = pre;
    20 
    21 while(true)
    22 
    23 {
    24 
    25      count = 0;
    26 
    27      p = pre;
    28 
    29      while(count < k && p != NULL)
    30 
    31      {
    32 
    33          p = p->next;
    34 
    35          ++ count;
    36 
    37      }
    38 
    39      if(p == NULL)
    40 
    41      {
    42 
    43          cout << "不存在倒数第 " << k << " 个结点" << endl;
    44 
    45          return NULL;
    46 
    47      }
    48 
    49      else if(p->next == NULL)
    50 
    51      {
    52 
    53          //找到了
    54 
    55          return pre;
    56 
    57      }
    58 
    59      else
    60 
    61      {
    62 
    63          //没找到
    64 
    65          pre = pre->next;
    66 
    67      }
    68 
    69 }  
    70 
    71 }

    简答题

    1. 差别:静态lib将各个调用函数都封装在生成的可执行文件中(.exe),而动态DLL则在需要时才动态地装载和所载DLL文件,动态DLL还可以利用到操作系统中既存的库文件。
    2. 处理:BeginPaint开始画,就是用白刷去掉原窗口,GetClientPaint获得窗口显示区域和尺寸等信息并绘制,EndPaint释放绘图句柄。
    3. 功能:都是用来解决共享变量或区域的访问问题,防止读写冲突。区别:Critical Section是在用户方式下实现同步,其他两个是系统内核对象。Mutex是只有获得锁的进程才能释放,而semaphore可由其他进程释放,一般mutex用于保护关键代码区域,而semaphore用于保护变量。
    4. 优点:多线程程序可以分时处理,用户因此可以同时高效地执行多个任务,用户体验好;缺点:线程切换有额外的代价,所以花费的总时间要长于单线程程序。
  • 相关阅读:
    GitLab 重置认证和添加账号缓存
    PHP 正则匹配IP
    git 删除指定版本
    PostgreSQL 9.2 日期运算
    postgre 已有字段修改为自增
    postgresql 导入导出
    PHP TS 和 NTS 版本选择
    background-image属性的设置
    SQLServer 附加数据库后只读或报错解决方法
    IIS 6.0 发布网站使用教程
  • 原文地址:https://www.cnblogs.com/jerry19880126/p/2623982.html
Copyright © 2011-2022 走看看