c/c++某大公司的兩道筆試題

才智咖 人氣:3.01W

1.一群人圍成一圈,123的報數,報3者出列,求最後一個人。

c/c++某大公司的兩道筆試題

2.利用連結串列實現將兩個有序佇列A和B合併到有序佇列H中,不準增加其他空間。

請提供全一點的程式

第一題:

// : Defines the entry point for the console application.

//

#include “stdafx.h”

#include “malloc.h”

#include “stdio.h”

int main()

{

int m,n;

int kickouts = 0;

int *p = NULL;

int i ,j;

i=j=0;

printf(“Please input m,n:”);

scanf(“%d,%d”,&m,&n);

while(n<1)

{

printf("n doen't less 0 , retry n:");

scanf("%d",&n);

}

p=(int*)malloc(m*sizeof(int));

for(i=0;i

p[i]=1;

i = 0;

while(1)

{

i = i%m;

if(p[i++]) j++;

if(j == n)

{

p[i-1]=0;

j = 0;

kickouts++;

}

if(kickouts == m-1)

break;

}

for(i = 0;i

if(p[i]) printf("%dn",i+1);

return 0;

}