C++ Exercises(十八)

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

 1.完成下列程序

    *

    *.*.

    *..*..*..

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

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

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

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

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

 

 

  
  
  
  
  1. #include <iostream>  
  2. using namespace std;  
  3. const int ROWS = 8;//行数  
  4.  
  5. void DoPrint()  
  6. {  
  7.     int i,j,k;  
  8.     for(i=1;i<=ROWS;++i)  
  9.     {  
  10.         for(j=1;j<=i;++j)  
  11.         {  
  12.             cout<<"*";  
  13.             for(k=1;k<=i-1;++k)cout<<".";  
  14.         }  
  15.         cout<<endl;  
  16.     }  
  17. }  
  18. int main()  
  19. {  
  20.     DoPrint();  
  21.     return 0;  

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

 

  
  
  
  
  1. #include <iostream>  
  2. using namespace std;  
  3.  
  4. void printArray(int a[],int n)  
  5. {  
  6.     for(int i=0;i<n;++i)  
  7.         cout<<a[i]<<"\t";  
  8.     cout<<endl;  
  9. }  
  10. int partion(int a[],int left,int right)     
  11. {//分割    
  12.     int temp = a[left];  
  13.     while (left<right)  
  14.     {  
  15.         while (left<right && a[right]<=temp)--right;  
  16.         a[left] = a[right];  
  17.         while (left<right && a[left]>=temp)++left;  
  18.         a[right] = a[left];  
  19.     }  
  20.     a[left] = temp;  
  21.     return   left;     
  22. }     
  23.  
  24. void quicksort(int a[],int left,int right)     
  25. {//快速排序    
  26.     if(left>=right)       
  27.         return;     
  28.     int pivotloc;     
  29.     pivotloc = partion(a,left,right);     
  30.     quicksort(a,left,pivotloc-1);     
  31.     quicksort(a,pivotloc+1,right);     
  32. }     
  33.  
  34. int main()  
  35. {  
  36.     int array[] = {45,56,76,234,1,34,23,2,3};  
  37.     int size = sizeof(array)/sizeof(array[0]);  
  38.     quicksort(array,0,size-1);  
  39.     printArray(array,size);  
  40.     return 0;  

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

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

  
  
  
  
  1. #include <iostream>  
  2. using namespace std;  
  3.  
  4. int Pheponatch(int N)  
  5. {//返回斐波那契数列第N项  
  6.     int a[100]={1,1};  
  7.     for (int i=2;i<N;++i)  
  8.     {  
  9.         a[i] = a[i-1]+a[i-2];  
  10.     }  
  11.     return a[N-1];  
  12. }  
  13. int main()  
  14. {   
  15.     cout<<Pheponatch(4)<<endl;  
  16.     return 0;  

 

 
4,错误原因:指针未初始化,
改正后的代码:
  
  
  
  
  1. #include <stdio.h>   
  2. #include <malloc.h>   
  3. #include <iostream>  
  4. using namespace std;  
  5.  
  6. struct TNode  
  7. {   
  8.     TNode* left;   
  9.     TNode* right;   
  10.     int value;   
  11. };   
  12.  
  13. TNode* root=NULL;   
  14.  
  15. void append(int N)   
  16. {   
  17.     TNode* NewNode=(TNode *)malloc(sizeof(TNode));   
  18.     NewNode->value=N;   
  19.     NewNode->left = NULL;  
  20.     NewNode->right = NULL;  
  21.     if(root==NULL)   
  22.     {  
  23.         root=NewNode;   
  24.         return;   
  25.     }   
  26.     else   
  27.     {   
  28.         TNode* temp;   
  29.         temp=root;  
  30.         while((N>=temp->value && temp->left!=NULL) || (N<temp->value && temp->right!=NULL))   
  31.         {   
  32.             while(N>=temp->value && temp->left!=NULL)   
  33.                 temp=temp->left;   
  34.             while(N<temp->value && temp->right!=NULL)   
  35.                 temp=temp->right;   
  36.         }   
  37.         if(N>=temp->value)   
  38.             temp->left=NewNode;   
  39.         else   
  40.             temp->right=NewNode;   
  41.         return;   
  42.     }   
  43. }  
  44.  
  45. void printTree(TNode* t)  
  46. {  
  47.     if (t!=NULL)  
  48.     {  
  49.         printf("%d\n",t->value);  
  50.         printTree(t->left);  
  51.         printTree(t->right);  
  52.     }  
  53. }  
  54. int main()   
  55. {   
  56.     append(63);   
  57.     append(45);   
  58.     append(32);   
  59.     append(77);   
  60.     append(96);   
  61.     append(21);   
  62.     append(17); // Again,  数字任意给出  
  63.     printTree(root);  
  64.     return 0;  
  65. }  

 

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

  
  
  
  
  1. #include <iostream>  
  2. using namespace std;  
  3.  
  4. int my_aoti(char* s)  
  5. {//字符串转化为整数  
  6.     int len = strlen(s);  
  7.     int result = 0;  
  8.     for (int i=0;i<len;++i)  
  9.     {  
  10.         result = result*10+s[i]-'0';  
  11.     }  
  12.     return result;  
  13. }  
  14. int main()   
  15. {   
  16.     char* str = "123";  
  17.     int num = my_aoti(str);  
  18.     cout<<num<<endl;  
  19.     return 0;  

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

 

  
  
  
  
  1. //双向链表节点  
  2. struct DbLinkNode  
  3. {  
  4.     struct DbLinkNode* prev;//前一个节点  
  5.     struct DbLinkNode* next;//后一个节点  
  6.     int value;  
  7. };  
  8.  
  9. bool Delete(DbLinkNode* head,int num)  
  10. {//在双向链表(带头节点)中删除第一个值为num的节点  
  11.     if (head->next!=NULL)  
  12.     {//表中有节点存在  
  13.         struct DbLinkNode* pre = head,p = head->next;  
  14.         while (p!=NULL&&p->value!=num)  
  15.         {  
  16.             pre = p;  
  17.             p = p->next;  
  18.         }  
  19.         if (p==NULL)  
  20.         {//没找到  
  21.             return false;  
  22.         }  
  23.         else if (p->next==NULL)  
  24.         {//待删除的是最后一个节点  
  25.             pre->next = NULL;  
  26.             delete p;  
  27.             p = NULL;  
  28.         }  
  29.         else 
  30.         {//待删除的不是最后一个  
  31.             pre->next = p->next;  
  32.             p->next->prev = pre;  
  33.             delete p;  
  34.             p = NULL;  
  35.         }      
  36.         return true;  
  37.     }  
  38.     else 
  39.     {  
  40.         return false;  
  41.     }  
  42.       
  43. }  
  44.  
  45. bool Insert(struct DbLinkNode* head,int target,int num)  
  46. {//在节点target后插入节点num  
  47.     if (head->next!=NULL)  
  48.     {//表中有节点存在  
  49.         struct DbLinkNode* p = head->next;//指向第一个节点  
  50.         struct DbLinkNode* newNode = (struct DbLinkNode*)malloc(sizeof(DbLinkNode));  
  51.         newNode->value = num;  
  52.         newNode->next = NULL;  
  53.         newNode->prev = NULL;  
  54.         while (p!=NULL && p->value!=num)  
  55.         {  
  56.             p = p->next;  
  57.         }  
  58.         if (p==NULL)  
  59.         {  
  60.             free(newNode);  
  61.             newNode = NULL;  
  62.             return false;  
  63.         }   
  64.         else if (p->next==NULL)  
  65.         {//目标节点是最后一个节点,新节点插入为尾节点  
  66.             p->next = newNode;  
  67.             newNode->prev = p;  
  68.         }  
  69.         else 
  70.         {//目标节点不是最后一个  
  71.             newNode->next = p->next;  
  72.             p->next->prev = newNode;  
  73.             p->next = newNode;  
  74.             newNode->prev = p;  
  75.         }  
  76.         return true;  
  77.     }  
  78.     else 
  79.     {  
  80.         return false;  
  81.     }  

 

你可能感兴趣的:(C++,职场,休闲,Exercises)