zoukankan      html  css  js  c++  java
  • 笔试

    1、下面两段代码中for循环分别执行了多少次?

    1. unsigned short i,j; 
    2. for(i=0, j=2; i!=j; i+=5, j+=7) 
    3. {} 

     

    1. unsigned short i,j; 
    2. for(i=3,j=7;i!=j;i+=3,j+=7) 

        对于这类问题可以利用相对运动的思想:

        根据数据类型可以知道环形轨道的长度是65536,对于第一个for循环假设i原地不动,则j在局里起点为2的位置以步长2向前运动,则j需要走(65536 - 2) / 2 = 32767步才能回到起点与i重合,此时循环退出,因此第一个for循环执行了32767次。

        对于第二个for循环轨道的长度依然是65536,我们以i的起始位置3为原点,假设i原地不动,则j在距离原点为4的位置以步长4向前远动,则j需走(65536 - 4) / 4 = 16383步才能回到起点与i重合退出循环,故第二个循环执行了16383次。

    2、下面程序应该输出多少?

    1. char *c[] = { "ENTER", "NEW", "POINT", "FIRST" };  
    2. char **cp[] = { c+3, c+2, c+1, c };  
    3. char ***cpp = cp;  
    4.   
    5. int main(void
    6. {  
    7.     printf("%s", **++cpp);  
    8.     printf("%s", *--*++cpp+3);  
    9.     printf("%s", *cpp[-2]+3);  
    10.     printf("%s\n", cpp[-1][-1]+1);  
    11.     return 0; 
    12. }

        画个图分析一下结果就会出来:

      打印结果:POINTERSTEW。

    3、下列代码的输出为多少?

    1. int main(void
    2.     enum {a, b=5, c, d=4, e}; 
    3.     enum {h,x, y, z, v=120, w, r=99,s,t}; 
    4.     return 0; 

    a、c、e、h、x、y、z、w、s、t的值分别是多少?

        枚举类型默认起始值为0,若里面元素初始化时没有赋值则取默认值,赋过值则取给定的值,元素的默认值总是它前面的那一个元素的值加1。

    结果:a = 0, c = 6, e = 5, h = 0, x = 1, y = 2, z = 3, w = 121, s = 100, t = 101

  • 相关阅读:
    【leetcode】Binary Search Tree Iterator
    【leetcode】Palindrome Partitioning II
    【leetcode】Best Time to Buy and Sell Stock III
    【leetcode】Best Time to Buy and Sell Stock II
    【leetcode】Longest Consecutive Sequence
    【leetcode】Factorial Trailing Zeroes
    【leetcode】Simplify Path
    【leetcode】Generate Parentheses
    【leetcode】Combination Sum II
    【leetcode】Combination Sum
  • 原文地址:https://www.cnblogs.com/ldjhust/p/2988305.html
Copyright © 2011-2022 走看看