课程咨询 :0571-56020834 QQ:1633630073

杭州达内T计划培训

杭州嵌入式培训 > 雇主企业 > 企业面试题 > 编写算法,从10亿个浮点数当中,选出其中最大的10000个
  • 编写算法,从10亿个浮点数当中,选出其中最大的10000个

    发布:杭州达内      来源:达内培训      时间:2015-09-14


  •  用外部排序,在《数据结构》书上有
    《计算方法导论》在找到第n大的数的算法上加工
    5、编写一unix程序,防止僵尸进程的出现.

    2.单连表的建立,把'a'--'z'26个字母插入到连表中,并且倒叙,还要打印!
    方法1:
    typedef struct val
    {    int date_1;
         struct val *next;
    }*p;
    void main(void)
    {    char c;
         
         for(c=122;c>=97;c--)
            { p.date=c;
              p="p-">next;
             }
         p.next=NULL;
    }
    }
    方法2:
    ode *p = NULL;
    ode *q = NULL;
    ode *head = (node*)malloc(sizeof(node));
    head->data = ' ';head->next=NULL;
    ode *first = (node*)malloc(sizeof(node));
    first->data = 'a';first->next=NULL;head->next = first;
    p = first;
    int longth = 'z' - 'b';
    int i="0";
    while ( i<=longth )
    {
    ode *temp = (node*)malloc(sizeof(node));
    temp->data = 'b'+i;temp->next=NULL;q=temp;
    head->next = temp; temp->next=p;p=q;
    i++;
    }
    print(head);
     
    测试程序
    #include
    struct A{
            char a;
            int b;
            unsigned short c;
            long d;
            unsigned long long e;
            char f;
    };

    struct B{
            char a;
            int b;
            unsigned short c;
            long d;
            unsigned long long e;
            char f;
    }__attribute__((aligned));

    struct C{
            char a;
            int b;
            unsigned short c;
            long d;
            unsigned long long e;
            char f;
    }__attribute__((aligned(1)));


    struct D{
            char a;
            int b;
            unsigned short c;
            long d;
            unsigned long long e;
            char f;
    }__attribute__((aligned(4)));

    struct E{
            char a;
            int b;
            unsigned short c;
            long d;
            unsigned long long e;
            char f;
    }__attribute__((aligned(8)));

    struct F{
            char a;
            int b;
            unsigned short c;
            long d;
            unsigned long long e;
            char f;
    }__attribute__((packed));

    int main(int argc, char **argv)
    {
            printf("A = %d, B = %d, C = %d, D = %d, E = %d, F = %d/n",
                    sizeof(struct A), sizeof(struct B), sizeof(struct C), sizeof(struct D), sizeof(struct E), sizeof(struct F));
            return 0;
    }
    在fedora 7下的测试结果:
    A = 28, B = 32, C = 28, D = 28, E = 32, F = 20
    A:不使用__attribute__ 默认4字节对齐
    B:__attribute__((aligned))
          the compiler automatically sets the alignment for the declared variable or field to the largest alignment which is ever used for any data type on the target machine you are compiling for. Doing this can often make copy operations more efficient, because the compiler can use whatever instructions copy the biggest chunks of memory when performing copies to or from the variables or fields that you have aligned this way.
       最大对齐方式,此例中为16字节对齐,同E
    C:__attribute__((aligned(1))) 不支持,除了packed 不能减小对齐字节数,以默认对齐方式对齐
    D:__attribute__((aligned(4))) 四字节对齐
    E:__attribute__((aligned(8))) 八字节对齐
    F:__attribute__((packed))
         the aligned attribute can only increase the alignment; but you can decrease it by specifying packed as well.
         The packed attribute specifies that a variable or structure field should have the smallest possible alignment―one byte for a variable, and one bit for a field, unless you specify a larger value with the aligned attribute.
    Here is a structure in which the field x is packed, so that it immediately follows a:
              struct foo
              {
                char a;
                int x[2] __attribute__ ((packed));
              };
       变量以字节对齐,结构体域以位对齐   
    cygwin下的测试结果:
    A = 32, B = 32, C = 32, D = 32, E = 32, F = 20
    从测试结果上看默认8字节对齐?或是只支持packed,未知
    可参考的文档:
    http://developer.apple.com/documentation/DeveloperTools/gcc-3.3/gcc/Variable-Attributes.html
    http://www.skynet.org.cn/archiver/?tid-87.html
    编程题

    一个递规反向输出字符串的例子,可谓是反序的经典例程.
    void inverse(char *p)
    {
         if( *p = = '/0' )
    return;
         inverse( p+1 );
         printf( "%c", *p );
    }
    int main(int argc, char *argv[])
    {
         inverse("abc/0");
         return 0;
    }

    3。输出和为一个给定整数的所有组合
    例如n=5
    5=1+4;5=2+3(相加的数不能重复)
    则输出
    1,4;2,3。
    答案:
    #i nclude
     #i nclude
    void main()
    {
    unsigned long int a,i=1;
    scanf("%d",&a);
    if(a%2==0)
    {
          for(i=1;i      printf("%d",a,a-i);
    }
    else
    for(i=1;i<=a/2;i++)
             printf(" %d, %d",i,a-i);
    }

    4。在对齐为4的情况下
    struct BBB
    {
        long num;
        char *name;
        short int data;
        char ha;
        short ba[5];
    }*p;
    p=0x1000000;
    p+0x200=____;
    (Ulong)p+0x200=____;
    (char*)p+0x200=____;
    答案:假设在32位CPU上,
    sizeof(long) = 4 bytes
    sizeof(char *) = 4 bytes
    sizeof(short int) = sizeof(short) = 2 bytes
    sizeof(char) = 1 bytes

    由于是4字节对齐,
    sizeof(struct BBB) = sizeof(*p)
    = 4 + 4 + 4((2 + 1 )+ 1补齐为4)+ 12(2*5 + 2补齐为12) = 24 bytes  
    p=0x1000000;
    p+0x200=____;
         = 0x1000000 + 0x200*24
    (Ulong)p+0x200=____;
         = 0x1000000 + 0x200
    (char*)p+0x200=____;
         = 0x1000000 + 0x200*4

    推荐文章

上一篇:用变量a给出下面的定义

下一篇:关键字const是什么含意?

最新开班日期  |  更多

嵌入式--免费训练营

嵌入式--免费训练营

开班日期:每月底

嵌入式--高薪就业班

嵌入式--高薪就业班

开班日期:每月底

嵌入式--零基础周末班

嵌入式--零基础周末班

开班日期:每月底

嵌入式--零基础全日制班

嵌入式--零基础全日制班

开班日期:每月底

  • 地址:杭州市西湖区文三路199号创业大厦
  • 课程培训电话:0571-56020834 QQ:1633630073     全国服务监督电话:400-827-0010
  • 服务邮箱 ts@tedu.cn
  • 2001-2016 达内国际公司(TARENA INTERNATIONAL,INC.) 版权所有 京ICP证08000853号-56