上海嵌入式培训
达内上海市场营销中心

15026646813

热门课程

编写算法,从10亿个浮点数当中,选出其中大的10000个

  • 时间:2018-03-05 15:58
  • 发布:上海嵌入式培训
  • 来源:企业面试题

用外部排序,在《数据结构》书上有
《计算方法导论》在找到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,未知
编程题

一个递规反向输出字符串的例子,可谓是反序的经典例程.
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是什么含意?

零基础的你如何学好嵌入式

嵌入式开发在工作中遇到的C知识要点

选择城市和中心
贵州省

广西省

海南省

免费学习5天VIP课程