C++ Exercises(十八)

下了份《 46家公司笔试题 》做做,好久没接触这些基本知识了,熟悉下

1.完成下列程序

*

*.*.

*..*..*..

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

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

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

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

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

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #include < iostream >
using namespace std;
const int ROWS = 8 ; // 行数

void DoPrint()
{
int i,j,k;
for (i = 1 ;i <= ROWS; ++ i)
{
for (j = 1 ;j <= i; ++ j)
{
cout
<< " * " ;
for (k = 1 ;k <= i - 1 ; ++ k)cout << " . " ;
}
cout
<< endl;
}
}
int main()
{
DoPrint();
return 0 ;
}

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

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #include < iostream >
using namespace std;

void printArray( int a[], int n)
{
for ( int i = 0 ;i < n; ++ i)
cout
<< a[i] << " /t " ;
cout
<< endl;
}
int partion( int a[], int left, int right)
{
// 分割
int temp = a[left];
while (left < right)
{
while (left < right && a[right] <= temp) -- right;
a[left]
= a[right];
while (left < right && a[left] >= temp) ++ left;
a[right]
= a[left];
}
a[left]
= temp;
return left;
}

void quicksort( int a[], int left, int right)
{
// 快速排序
if (left >= right)
return ;
int pivotloc;
pivotloc
= partion(a,left,right);
quicksort(a,left,pivotloc
- 1 );
quicksort(a,pivotloc
+ 1 ,right);
}

int main()
{
int array[] = { 45 , 56 , 76 , 234 , 1 , 34 , 23 , 2 , 3 };
int size = sizeof (array) / sizeof (array[ 0 ]);
quicksort(array,
0 ,size - 1 );
printArray(array,size);
return 0 ;
}

3,费波那其数列,1,1,2,3,5……编写程序求第十项。可以用递归,也可以用其他

方法,但要说明你选择的理由。

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #include < iostream >
using namespace std;

int Pheponatch( int N)
{
// 返回斐波那契数列第N项
int a[ 100 ] = { 1 , 1 };
for ( int i = 2 ;i < N; ++ i)
{
a[i]
= a[i - 1 ] + a[i - 2 ];
}
return a[N - 1 ];
}
int main()
{
cout
<< Pheponatch( 4 ) << endl;
return 0 ;
}

4,错误原因:指针未初始化,

改正后的代码:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #include < stdio.h >
#include
< malloc.h >
#include
< iostream >
using namespace std;

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

TNode
* root = NULL;

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) || (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 printTree(TNode * t)
{
if (t != NULL)
{
printf(
" %d/n " ,t -> value);
printTree(t
-> left);
printTree(t
-> right);
}
}
int main()
{
append(
63 );
append(
45 );
append(
32 );
append(
77 );
append(
96 );
append(
21 );
append(
17 ); // Again,数字任意给出
printTree(root);
return 0 ;
}

5,设计函数int atoi(char *s) 。

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #include < iostream >
using namespace std;

int my_aoti( char * s)
{
// 字符串转化为整数
int len = strlen(s);
int result = 0 ;
for ( int i = 0 ;i < len; ++ i)
{
result
= result * 10 + s[i] - ' 0 ' ;
}
return result;
}
int main()
{
char * str = " 123 " ;
int num = my_aoti(str);
cout
<< num << endl;
return 0 ;
}

6,实现双向链表删除一个节点P,在节点P 后插入一个节点,写出这两个函数

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> // 双向链表节点
struct DbLinkNode
{
struct DbLinkNode * prev; // 前一个节点
struct DbLinkNode * next; // 后一个节点
int value;
};

bool Delete(DbLinkNode * head, int num)
{
// 在双向链表(带头节点)中删除第一个值为num的节点
if (head -> next != NULL)
{
// 表中有节点存在
struct DbLinkNode * pre = head,p = head -> next;
while (p != NULL && p -> value != num)
{
pre
= p;
p
= p -> next;
}
if (p == NULL)
{
// 没找到
return false ;
}
else if (p -> next == NULL)
{
// 待删除的是最后一个节点
pre -> next = NULL;
deletep;
p
= NULL;
}
else
{
// 待删除的不是最后一个
pre -> next = p -> next;
p
-> next -> prev = pre;
deletep;
p
= NULL;
}
return true ;
}
else
{
return false ;
}

}

bool Insert( struct DbLinkNode * head, int target, int num)
{
// 在节点target后插入节点num
if (head -> next != NULL)
{
// 表中有节点存在
struct DbLinkNode * p = head -> next; // 指向第一个节点
struct DbLinkNode * newNode = ( struct DbLinkNode * )malloc( sizeof (DbLinkNode));
newNode
-> value = num;
newNode
-> next = NULL;
newNode
-> prev = NULL;
while (p != NULL && p -> value != num)
{
p
= p -> next;
}
if (p == NULL)
{
free(newNode);
newNode
= NULL;
return false ;
}
else if (p -> next == NULL)
{
// 目标节点是最后一个节点,新节点插入为尾节点
p -> next = newNode;
newNode
-> prev = p;
}
else
{
// 目标节点不是最后一个
newNode -> next = p -> next;
p
-> next -> prev = newNode;
p
-> next = newNode;
newNode
-> prev = p;
}
return true ;
}
else
{
return false ;
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值