I come, I see, I conquer

                    —Gaius Julius Caesar

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

1.完成下列程序
*
*.*.
*..*..*..
*...*...*...*...
*....*....*....*....*....
*.....*.....*.....*.....*.....*.....
*......*......*......*......*......*......*......
*.......*.......*.......*.......*.......*.......*.......*.......

#include <stdio.h> 
#define N 8 
int main() 
{
    
int i;
    
int j; 
    
int k;
    
---------------------------------------------------------
    
| | 
    
| | 
    
| | 
    
--------------------------------------------------------- 
    
return 0
}

 

2.完成程序,实现对数组的降序排序

#include <stdio.h>

void sort();
int main() 
{
    
int array[]={45,56,76,234,1,34,23,2,3}; //数字任//意给出
    sort(); 
    
return 0

void sort() 
{
    
-----------------------------------------------------
 
| | 
 
| | 
 
| |
    
-----------------------------------------------------
}

 

3.费波那其数列,1,1,2,3,5……编写程序求第十项。可以用递归,也可以用其他方法,但要说明你选择的理由。

#include <stdio.h> 

int Pheponatch(int); 
int main() 
{
    printf(
"The 10th is %d",Pheponatch(10)); 
    
return 0

int Pheponatch(int N) 

    
-------------------------------- 
    
| | 
    | | 
 
-------------------------------- 
}

 

4.下列程序运行时会崩溃,请找出错误并改正,并且说明原因。

#include <stdio.h> 
#include 
<malloc.h> 

typedef 
struct

    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, 数字任意给出 


void append(int N) 

    TNode
* NewNode=(TNode *)malloc(sizeof(TNode)); 
    NewNode
->value=N; 

    
if(root==NULL) 
    { 
        root
=NewNode;
        
return;
    } 
    
else 
    { 
        TNode
* temp; 
        temp
=root; 
        
while((N>=temp.value && temp.left!=NULL) || (N<temp. value && temp. right!=NULL)) 
        { 
            
while(N>=temp.value && temp.left!=NULL)
                temp
=temp.left;
            
while(N<temp.value && temp.right!=NULL)
                temp
=temp.right;
        }
        
if(N>=temp.value)
            temp.left
=NewNode; 
        
else
            temp.right
=NewNode;
        
return
    }
}

 

下面是我做的答案,如有问题,欢迎和我交流!
 
1.题目要求打印一个图形,可以看出此图形的规律是第i行(i=1,2,3,...8)由i个'*'后跟i-1个'.'的重复序列构成,重复的次数为i次.
程序代码如下:

#include <stdio.h>
#define N 8

int main()
{
    
int i;    /* control the number of lines to draw  */
    
int j;      /* control the drawing of '*' each line */
    
int k;    /* control the drawing of '.' following '*' each line */
    
    
for (i = 1; i <= N; i++)
    {
        
for (j = 0; j < i; j++)
        {
            putchar(
'*');
            
            
for (k = 0; k < i; k++)
                putchar(
'.');
        }
        putchar(
'\n');
    }
    
    
return 0;
}

 

2.题目中的程序可能有些问题,我给sort()函数加上了2个参数,变为sort(int a[],int size),另外还添加了一个打印函数,以方便测试结果程序代码如下:

#include <stdio.h>

void sort(int a[], int size);
void print(int a[], int size);

int main()
{
    
int array[] = {4556762341342323};
    
int size = sizeof(array) / sizeof(int);
    
    printf(
"排序前:\n");
    print(array, size);
    
    sort(array, size);
    printf(
"排序后:\n");
    print(array, size);
    
    
return 0;
}

/* a simple select sort */
void sort(int a[], int size)
{
    
int i;
    
int j;
    
int max = 0/* hold the index of max element*/
    
int tmp;
    
    
for (i = 0; i < size; i++)
    {
        max 
= i;
        
        
for (j = i; j < size; j++)
            
if (a[j] > a[max])
            {
                tmp 
= a[j];
                a[j] 
= a[max];
                a[max] 
= tmp;
            }
    }
}

void print(int a[], int size)
{
    
int i;
    
for (i = 0; i < size; i++)
        printf(
"%d ", a[i]);
    
    printf(
"\n");
}

 

3.分别给出了费波那其数列递归和非递归的解法, 递归程序需要的空间开销比较大,而且其中含有多次重复计算,所以效率上没非递归高,但是程序简洁

#include <stdio.h>

int Pheponatch(int);
int Pheponatch_recur(int);

int main()
{
    
int i;

    
for (i = 0; i < 10; i++)
        printf(
"%d ", Pheponatch_recur(i));

    printf(
"\n");
    printf(
"The 10th is %d", Pheponatch_recur(10));

    
return 0;
}

/* 
 * an iteration version 
 * reduce some redundant computation comparied with
 * recursive implementation
 
*/
int Pheponatch(int N)
{
    
int f0 = 1;
    
int f1 = 1;
    
int f2;

    
if (N < 2return 1;

    
while ( --N )
    {
        f2 
= f0 + f1;
        f0 
= f1;
        f1 
= f2;
    }

    
return f2;

}


/* 
 * recursive version 
 * simple implementation ,but may lose some efficiency
 * since it has some repeat computation
 
*/

int Pheponatch_recur(int N)
{
    
if (N < 2return 1;
    
else 
        
return Pheponatch_recur(N-2+ Pheponatch_recur(N-1);
}

 

4.此程序的功能是完成一个有序二叉树的建立,使得左子树结点的值小于根结点,右子树大于根结点. 题目程序中结构体定义的地方有误,在TNode名字出现之前,就在结构体内使用TNode,将导致编译错误.另外题目中append函数中的所有temp的点号操作符都应改成->,因为temp是个指针.至于题目中所说的程序在运行时崩溃出现在append函数中的对temp->left和temp->right操作时候, 因为每创建一个新结点的时候都没将left和right成员初始化为空指针,会导致append函数中的while循环访问到非法内存,从而导致程序崩溃. 为了方便测试,添加了函数print_tree打印树的结点内容,另外补充了一个函数free_tree来释放动态分配的内存.修改后的程序如下:

#include <stdio.h>
#include 
<stdlib.h>
#include 
<malloc.h>

struct tag_Node
{
    
struct tag_Node *left;
    
struct tag_Node *right;
    
int value;
};
typedef 
struct tag_Node  TNode;

TNode 
*root = NULL;

void append(int N);
void free_tree(TNode *root);
void print_tree(TNode *root);


int main()
{
    append(
63);
    append(
45);
    append(
32);
    append(
77);
    append(
96);
    append(
21);
    append(
17); // Again, 数字任意给出

    print_tree(root);
    printf(
"\n");

    free_tree(root);
    printf(
"memory released!\n");


    
return 0;
}


void append(int N)
{
    TNode
* NewNode = (TNode *)malloc(sizeof(TNode));
    
if ( !NewNode )
    {
        printf(
"memory alloc failure!\n");
        exit(
1);
    } 
/* 以下两行用来初始化每个新结点的left和right成员   如遗漏这两句,会导致程序运行时崩溃 */
    NewNode
->left = NULL;
    NewNode
->right = NULL;
    NewNode
->value=N;
    
if(root==NULL)
    {
        root 
= NewNode;
        
return;
    }
    
else
    {
        TNode
* temp;
        temp
=root;
        
while((N >= temp->value && temp->left != NULL) ||
                (N 
< temp->value && temp->right != NULL))
        {

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

            
while(N < temp->value && temp->right != NULL)
                temp 
= temp->right;

        }

        
if(N >= temp->value)
            temp
->left  =NewNode;
        
else
            temp
->right = NewNode;
        
return;
    }
}


void free_tree(TNode *root)
{
    
if ( !root->left && !root->right)
    {
        free(root);
        
return;
    }
    
if (root->left)
        free_tree(root
->left);
    
else
        free_tree(root
->right);
}

void print_tree(TNode *root)
{
    
if ( !root->left && !root->right)
    {
        printf(
"%d ", root->value);
        
return ;
    }

    
if (root->right)
        print_tree(root
->right);

    printf(
"%d ", root->value);

    
if (root->left)
        print_tree(root
->left);
}

 

本文选自:http://blog.chinaunix.net/u/19481/showart_138224.html

posted on 2008-09-12 21:23  jcsu  阅读(1077)  评论(0)    收藏  举报