Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4744930
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: C/C++

2009-06-28 14:17:52

1、线形表a、b为两个有序升序的线形表,编写一程序,使两个有序线形表合并成一个有序升序线形表h;


答案在 请化大学 严锐敏《数据结构第二版》第二章例题,数据结构当中,这个叫做:两路归并排序
Linklist *unio(Linklist *p,Linklist *q){
linklist *R,*pa,*qa,*ra;
pa=p;
qa=q;
R=ra=p;
while(pa->next!=NULL&&qa->next!=NULL){
if(pa->data>qa->data){
ra->next=qa;
qa=qa->next;
}
else{
ra->next=pa;
pa=pa->next;
}
}
if(pa->next!=NULL)
ra->next=pa;
if(qa->next!=NULL)
ra->next==qa;
return R;
}
2、运用四色定理,为N个局域举行配色,颜色为1、2、3、4四种,另有数组adj[][N],如adj[i][j]=1则表示i区域与j区域相邻,数组color[N],如color[i]=1,表示i区域的颜色为1号颜色。
四色填充
3、用递归算法判断数组a[N]是否为一个递增数组。
递归的方法,记录当前最大的,并且判断当前的是否比这个还大,大则继续,否则返回false结束:
bool fun( int a[], int n )
{
if( n= =1 )
return true;
if( n= =2 )
return a[n-1] >= a[n-2];
return fun( a,n-1) && ( a[n-1] >= a[n-2] );
}
4、编写算法,从10亿个浮点数当中,选出其中最大的10000个。
用外部排序,在《数据结构》书上有
《计算方法导论》在找到第n大的数的算法上加工
5、编写一unix程序,防止僵尸进程的出现.
同学的4道面试题,应聘的职位是搜索引擎工程师,后两道超级难,(希望大家多给一些算发)
1.给两个数组和他们的大小,还有一动态开辟的内存,求交集,把交集放到动态内存dongtai,并且返回交集个数
long jiaoji(long* a[],long b[],long* alength,long blength,long* dongtai[])
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:
node *p = NULL;
node *q = NULL;

node *head = (node*)malloc(sizeof(node));
head->data = ' ';head->next=NULL;

node *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 )
{
node *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);

6.可怕的题目终于来了
象搜索的输入信息是一个字符串,统计300万输入信息中的最热门的前十条,我们每次输入的一个字符串为不超过255byte,内存使用只有1G,
请描述思想,写出算发(c语言),空间和时间复杂度,
7.国内的一些帖吧,如baidu,有几十万个主题,假设每一个主题都有上亿的跟帖子,怎么样设计这个系统速度最好,请描述思想,写出算发(c语言),空间和时间复杂度,


#include    string.h
main(void)
{    char    *src="hello,world";
     char    *dest=NULL;
     dest=(char    *)malloc(strlen(src));
     int    len=strlen(str);
     char    *d=dest;
     char    *s=src[len];
     while(len--!=0)
       d++=s--;
     printf("%s",dest);
}
找出错误!!
#include    "string.h"
#include "stdio.h"
#include "malloc.h"
main(void)
{   
char    *src="hello,world";
     char    *dest=NULL;
     dest=(char    *)malloc(sizeof(char)*(strlen(src)+1));
     int    len=strlen(src);
     char    *d=dest;
     char    *s=src+len-1;
     while(len--!=0)
       *d++=*s--;
*d='\0';
     printf("%s",dest);
}

intel c语言

1。struct s1
{
   int i: 8;
   int j: 4;
   int a: 3;
   double b;
};

struct s2
{
   int i: 8;
   int j: 4;
   double b;
   int a:3;
};

printf("sizeof(s1)= %d\n", sizeof(s1));
printf("sizeof(s2)= %d\n", sizeof(s2));

答案:16, 24
第一个struct s1
{
   int i: 8;
   int j: 4;
   int a: 3;
   double b;
};
理论上是这样的,首先是i在相对0的位置,占8位一个字节,然后,j就在相对一个字节的位置,由于一个位置的字节数是4位的倍数,因此不用对齐,就放在那 里了,然后是a,要在3位的倍数关系的位置上,因此要移一位,在15位的位置上放下,目前总共是18位,折算过来是2字节2位的样子,由于double是 8字节的,因此要在相对0要是8个字节的位置上放下,因此从18位开始到8个字节之间的位置被忽略,直接放在8字节的位置了,因此,总共是16字节。

第二个最后会对照是不是结构体内最大数据的倍数,不是的话,会补成是最大数据的倍数

编程题

2。读文件file1.txt的内容(例如):
12
34
56
输出到file2.txt:
56
34
12
(逆序)

答案:注意可增长数组的应用.
#include
#include


3。输出和为一个给定整数的所有组合
例如n=5
5=1+4;5=2+3(相加的数不能重复)
则输出
1,4;2,3。

答案:

#include 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

int main(void)
{
unsigned long int i,j,k;

printf("please input the number\n");
scanf("%d",&i);
     if( i % 2 == 0)
         j = i / 2;
else
j = i / 2 + 1;

printf("The result is \n");
     for(k = 0; k < j; k++)
      printf("%d = %d + %d\n",i,k,i - k);
return 0;
}

#include
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);
}

一个递规反向输出字符串的例子,可谓是反序的经典例程.

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;
}

由于是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

5。 写一段程序,找出数组中第k大小的数,输出数所在的位置。例如{2,4,3,4,7}中,第一大的数是7,位置在4。第二大、第三大的数都是4,位置在 1、3随便输出哪一个均可。函数接口为:int find_orderk(const int* narry,const int n,const int k)
要求算法复杂度不能是O(n^2)

答案:可以先用快速排序进行排序,其中用另外一个进行地址查找
代码如下,在VC++6.0运行通过。给分吧^-^

//快速排序

#include

usingnamespacestd;

intPartition (int*L,intlow,int high)
{
inttemp = L[low];
intpt = L[low];

while (low < high)
{
while (low < high && L[high] >= pt)
--high;
L[low] = L[high];
while (low < high && L[low] <= pt)
++low;
L[low] = temp;
}
L[low] = temp;

returnlow;
}

voidQSort (int*L,intlow,int high)
{
if (low < high)
{
intpl = Partition (L,low,high);

QSort (L,low,pl - 1);
QSort (L,pl + 1,high);
}
}

intmain ()
{
intnarry[100],addr[100];
intsum = 1,t;

cout << "Input number:" << endl;
cin >> t;

while (t != -1)
{
narry[sum] = t;
addr[sum - 1] = t;
sum++;

cin >> t;
}

sum -= 1;
QSort (narry,1,sum);

for (int i = 1; i <= sum;i++)
cout << narry[i] << '\t';
cout << endl;

intk;
cout << "Please input place you want:" << endl;
cin >> k;

intaa = 1;
intkk = 0;
for (;;)
{
if (aa == k)
break;
if (narry[kk] != narry[kk + 1])
{
aa += 1;
kk++;
}

}

cout << "The NO." << k << "number is:" << narry[sum - kk] << endl;
cout << "And it's place is:" ;
for (i = 0;i < sum;i++)
{
if (addr[i] == narry[sum - kk])
cout << i << '\t';
}


return0;
}

int main(void)
{
          int MAX = 10;
int *a = (int *)malloc(MAX * sizeof(int));
int *b;
    
FILE *fp1;
FILE *fp2;

fp1 = fopen("a.txt","r");
if(fp1 == NULL)
{printf("error1");
     exit(-1);
}

     fp2 = fopen("b.txt","w");
if(fp2 == NULL)
{printf("error2");
     exit(-1);
}

int i = 0;
     int j = 0;

while(fscanf(fp1,"%d",&a[i]) != EOF)
{
i++;
j++;
if(i >= MAX)
{
MAX = 2 * MAX;
b = (int*)realloc(a,MAX * sizeof(int));
if(b == NULL)
{
printf("error3");
exit(-1);
}
a = b;
}
}

for(;--j >= 0;)
    fprintf(fp2,"%d\n",a[j]);

fclose(fp1);
fclose(fp2);

return 0;
}

6。void g(int**);
int main()
{
int line[10],i;
int *p=line;
for (i=0;i<10;i++)
{
*p=i;
g(&p);
}
for(i=0;i<10;i++)
printf("%d\n",line[i]);
return 0;
}

void g(int**p)
{
(**p)++;
(*p)++;
}
输出?

答案:1
2
3
4
5
6
7
8
9
10这道题目不是很好理解,我们可以把题目g函数改为

void g(int**s)
{
(**s)++;
(*s)++;
}

这 样不容易看混淆s就是一个形式参数,g(&p)输入的就是p的地址,这样有如下指向:*p-》p-》line调用函数时候,**s对应*p即数组 中的数,*s对应p即数组数字的地址,这样就可以知道了(**s)++是line中的内容增加;(*s)++是地址增加。

7。写出程序运行结果

int sum(int a)
{
auto int c=0;
static int b=3;
c+=1;
b+=2;
return(a+b+c);
}

void main()
{
int I;
int a=2;
for(I=0;I<5;I++)
{
printf("%d,", sum(a));
}
}

答案:static会保存上次结果,记住这一点,剩下的自己写
输出:8,10,12,14,16,

8。int a[3];
a[0]=0; a[1]=1; a[2]=2;
int *p, *q;
p=a;
q=&a[2];
则a[q-p]=a[2]

答案:指针一次移动一个int但计数为1,p=a;q=a+2。

1。分析下面的程序:
void GetMemory(char **p,int num)
{
     *p=(char *)malloc(num);
    
}        
int main()
{
     char *str=NULL;
    
     GetMemory(&str,100);
    
     strcpy(str,"hello");
    
     free(str);
    
     if(str!=NULL)
     {
         strcpy(str,"world");
     }    
        
     printf("\n str is %s",str);
     getchar();
}    
问输出结果是什么?

答案:输出str is world。
free 只是释放的str指向的内存空间,它本身的值还是存在的.
所以free之后,有一个好的习惯就是将str=NULL.
此时str指向空间的内存已被回收,如果输出语句之前还存在分配空间的操作的话,这段存储空间是可能被重新分配给其他变量的,
尽管这段程序确实是存在大大的问题(上面各位已经说得很清楚了),但是通常会打印出world来。
这是因为,进程中的内存管理一般不是由操作系统完成的,而是由库函数自己完成的。
当你malloc一块内存的时候,管理库向操作系统申请一块空间(可能会比你申请的大一些),然后在这块空间中记录一些管理信息(一般是在你申请的内存前 面一点),并将可用内存的地址返回。但是释放内存的时候,管理库通常都不会将内存还给操作系统,因此你是可以继续访问这块地址的,只不过。。。。。。。。 楼上都说过了,最好别这么干。

2。运行的结果为什么等于15

#include "stdio.h"
#include "string.h"

void main()
{

char aa[10];
printf("%d",strlen(aa));
}

答案:sizeof()和初不初始化,没有关系;strlen()和初始化有关。

3。给定结构struct A
{
        char t:4;
        char k:4;
        unsigned short i:8;
        unsigned long m;
};问sizeof(A) = ?

答案:给定结构

struct A
{
        char t:4; //4位
        char k:4; //4位
        unsigned short i:8; //8位 这里要偏移2字节保证4字节对齐    
        unsigned long m; //4个字节
}; // 共8字节

4。分析一下
#include
#include
#include
#include
#include
#include
typedef struct   AA
{
         int b1:5;
         int b2:2;
}AA;
void main()
{
        AA aa;
        char cc[100];
        strcpy(cc,"0123456789abcdefghijklmnopqrstuvwxyz");
        memcpy(&aa,cc,sizeof(AA));
        cout << aa.b1 <         cout << aa.b2 < }

答案: -16和1
首先sizeof(AA)的大小为4,b1和b2分别占5bit和2bit.
经过strcpy和memcpy后,aa的4个字节所存放的值是:
0,1,2,3的ASC码,即00110000,00110001,00110010,00110011
所以,最后一步:显示的是这4个字节的前5位,和之后的2位
分别为:10000,和01
因为int是有正负之分  所以是-16和1

5。求函数返回值,输入x=9999;
int func ( x )
{
     int countx = 0;
     while ( x )
     {
         countx ++;
         x = x&(x-1);
     }
     return countx;
}
结果呢?

答案:知道了这是统计9999的二进制数值中有多少个1的函数,且有
9999=9×1024+512+256+15

9×1024中含有1的个数为2;
512中含有1的个数为1;
256中含有1的个数为1;
15中含有1的个数为4;
故共有1的个数为8,结果为8。
1000 - 1 = 0111,正好是原数取反。这就是原理。
用这种方法来求1的个数是很效率很高的。
不必去一个一个地移位。循环次数最少。

6。int a,b,c 请写函数实现C=a+b ,不可以改变数据类型,如将c改为long int,关键是如何处理溢出问题
答案:bool add (int a, int b,int *c)
{
*c=a+b;
return (a>0 && b>0 &&(*ca || *c>b)));
}

7。分析:
struct bit
{    int a:3;
     int   b:2;
     int c:3;
};
int main()
{
   bit s;
   char *c=(char*)&s;
    cout<    *c=0x99;
    cout << s.a <       int a=-1;
    printf("%x",a);
   return 0;
}
输出为什么是?

答案:4
1
-1
-4
ffffffff
因为0x99在内存中表示为 100 11 001 , a = 001, b = 11, c = 100
(在vc环境中,一般是由右到左进行分配的)
当c为有符合数时, c = 100, 最高1为表示c为负数,负数在计算机用补码表示,所以c = -4;同理
b = -1;
当c为有符合数时, c = 100,即 c = 4,同理 b = 3

8。改错:
#include

int main(void) {

     int **p;
     int arr[100];

     p = &arr;

     return 0;
}

答案:搞错了,是指针类型不同,
int **p; //二级指针
&arr; //得到的是指向第一维为100的数组的指针
应该这样写#include
int main(void) {
int **p, *q;
int arr[100];
q = arr;
p = &q;
return 0;

9。下面这个程序执行后会有什么错误或者效果:
#define MAX 255
int main()
{
    unsigned char A[MAX],i; //i被定义为unsigned char
    for (i=0;i<=MAX;i++)
       A[i]=i;
}

答案:死循环加数组越界访问(C/C++不进行数组越界检查)
MAX=255
数组A的下标范围为:0..MAX-1,这是其一..
其二.当i循环到255时,循环内执行:
   A[255]=255;
这句本身没有问题..但是返回for (i=0;i<=MAX;i++)语句时,
由于unsigned char的取值范围在(0..255),i++以后i又为0了..无限循环下去.

11。struct name1{
    char   str;
    short x;
    int    num;
}

struct name2{
    char str;
    int num;
    short x;
}

sizeof(struct name1)=??,sizeof(struct name2)=??

答案:sizeof(struct name1)=8,sizeof(struct name2)=12
在第二个结构中,为保证num按四个字节对齐,char后必须留出3字节的空间;同时为保证整个结构的自然对齐(这里是4字节对齐),在x后还要补齐2个字节,这样就是12字节。

阅读(1585) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~