笔试算法题(02):N阶阶乘 & 双向循环链表实现

出题:N阶阶乘问题的递归算法和非递归算法;

分析:

  • 第一种解法:普通暴力解法的实现较为容易;
  • 第二种解法:stirling公式可快速给出近似解;

解题:

 1 int Recursive(int s) {
 2         if(1 == s) {
 3                 return s;
 4         }
 5         return s*Recursive(s-1);
 6 }
 7 int NonRecursive(int s) {
 8         int temp=1;
 9         int result=1;
10         while(temp<=s) {
11                 result*=temp;
12                 temp++;
13         }
14         return result;
15 }
16 int main() {
17         int s=9;
18         printf("recursive output: %d\n",Recursive(s));
19         printf("non-recursive output: %d",NonRecursive(s));
20 }

出题:双向循环链表的插入,查找与删除;

分析:构造类的过程中需要进行的核查

解题:

 1 #include <stdio.h>
 2 /**
 3  * 检查是否需要构造函数
 4  * 检查是否需要无参构造函数
 5  * 检查是否需要成员变量(函数)私有
 6  * 检查是否需要在使用成员初始化列表
 7  * 检查是否需要析构函数
 8  * 检查是否需要虚拟析构函数
 9  * 检查是否需要复制构造函数(参数为const)
10  * 检查是否需要赋值重载函数(参数为const)
11  *
12  * */
13 class Node {
14 public:
15         int value;
16         Node *next;
17         Node *last;
18         Node(int v=0): next(NULL),last(NULL) {
19 
20         }
21         ~Node() {
22                 if(next!=NULL) delete next;
23                 if(last!=NULL) delete last;
24         }
25 };
26 class DDList {
27 public:
28         Node *head;
29         DDList():head(new Node(0)) {
30                 head->last=head;
31                 head->next=head;
32         }
33         DDList(const DDList& target) {}
34         DDList& operator=(const DDList& target) {}
35         void insertN(int v) {
36                 Node *newn=new Node(v);
37                 Node *temp;
38                 temp=head->next;
39                 head->next=newn;
40                 newn->next=temp;
41                 newn->last=head;
42                 temp->last=newn;
43         }
44         bool deleteN(int v) {
45                 Node *target=queryN(v);
46                 Node *temp;
47                 if(target == NULL) return false;
48                 temp=target->last;
49                 temp->next=target->next;
50                 target->next->last=temp;
51                 delete target;
52                 return true;
53         }
54         Node* queryN(int v) {
55                 Node *temp=head->next;
56                 while(temp != head) {
57                         if(temp->value == v) return temp;
58                         temp=temp->next;
59                 }
60                 return NULL;
61         }
62         void destroy() {
63                 if(head->next == head) return;
64                 deleteN(head->next->value);
65         }
66         void printList() {
67                 Node *temp=head->next;
68                 while(temp != head) {
69                         printf("current point: %d",temp->value);
70                         temp=temp->next;
71                 }
72         }
73         ~DDList() {
74                 if(head->last == head) delete head;
75                 else destroy();
76         }
77 };

转载于:https://www.cnblogs.com/leo-chen-2014/p/3730530.html

猜你喜欢

转载自blog.csdn.net/weixin_33728708/article/details/94232226