當前位置:學問君>學習教育>考研>

Sony技術類筆試題及分析

學問君 人氣:1.62W

   1.完成下列程序

Sony技術類筆試題及分析

*

*.*.

*..*..*..

*...*...*...*...

*....*....*....*....*....

*.....*.....*.....*.....*.....*.....

*......*......*......*......*......*......*......

*.......*.......*.......*.......*.......*.......*.......*.......

#include

#define N 8

int main()

{

int i;

int j;

int k;

---------------------------------------------------------

││││││

---------------------------------------------------------

return 0;

}

答:

#define N 8

int main()

{

int i;

int j;

int k;

for(i=0;i{

for(j=0;j{

printf("*");

for(k=0;kprintf(".");

}

printf("n");

}

return 0;

}

2.完成程序,實現對數組的'降序排序

#include

void sort( );

int main()

{

int array[]={45,56,76,234,1,34,23,2,3}; //數字任意給出

sort( );

return 0;

}

void sort( )

{

---------------------------------------------------------

││││││

---------------------------------------------------------

}

答:使用選擇排序法,我爲sort函數多加了兩個形參,至少第一個是必須的,否則無法傳入待排序數組。不知道這樣做是否符合題意。

void sort(int *array,int num)

{

int temp;

for(int i=0;ifor(int j=i+1;jif (array{

temp=array;

array=array[j];

array[j]=temp;

}

}

3.菲波納契數列,1,1,2,3,5……編寫程序求第十項。可以用遞歸,也可以用其他方法,但要說明你選擇的理由。

#include

int Pheponatch(int);

int main()

{

printf("The 10th is %d",Pheponatch(10));

return 0;

}

int Pheponatch(int N)

{

--------------------------------

││││

--------------------------------

}

答:使用遞歸,理由是遞歸編程簡單,代碼容易理解,但缺點是效率不高,而且有深度限制,如果深度太深,則堆棧會溢出。

int Pheponatch(int N)

{

if (N==3)

return 2;

else if (N==2||N==1)

return 1;

else

return Pheponatch(N-1)+Pheponatch(N-2);

}

4.下列程序執行時會崩潰,請找出錯誤並改正,並且說明原因。

#include

#include

typedef struct TNode

{

TNode* left;

TNode* right;

int value;

}TNode;

TNode* root=NULL;

void append(int N);

int main()

{

append(63);

append(45);

append(32);

append(77);

append(96);

append(21);

append(17); // Again, 數字任意給出

return 0;

}

void append(int N)

{

TNode* NewNode=(TNode *)malloc(sizeof(TNode));

NewNode->value=N;

NewNode->left=NULL; //新增

NewNode->right=NULL; //新增

if(root==NULL)

{

root=NewNode;

return;

}

else

{

TNode* temp;

temp=root;

while((N>=temp->value && temp->left!=NULL)||(Nvalue && temp->right!=NULL))

{

while(N>=temp->value && temp->left!=NULL)

temp=temp->left;

while(Nvalue && temp->right!=NULL)

temp=temp->right;

}

if(N>=temp->value)

temp->left=NewNode;

else

temp->right=NewNode;

return;

}

}

答:因爲新節點的左右指針沒有賦NULL值,至使下面的while循環不能正確結束而導致內存越界,最後崩潰(注意結束條件是temp->left!= NULL或temp->right!=NULL)。改正就是增加兩條賦值語句。