2014小米研发笔试

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <cstring>  
  4. #include <vector>  
  5. #include <algorithm>  
  6. using namespace std;  
  7.   
  8. struct Node{  
  9.     Node *left;  
  10.     Node *right;  
  11.     Node * sibling;  
  12.     int value;  
  13. };  
  14.   
  15. void createTree(Node *&root){  
  16.     int x;  
  17.     cin >> x;  
  18.     if(x == 0)  
  19.         root = NULL;  
  20.     else{  
  21.         root = new Node;  
  22.         root->sibling = NULL;  
  23.         root->value = x;  
  24.         createTree(root->left);  
  25.         createTree(root->right);  
  26.     }  
  27. }  
  28.   
  29. void printTree(Node *root){  
  30.     if(root){  
  31.         cout << root->value << " ";  
  32.         printTree(root->left);  
  33.         printTree(root->right);  
  34.     }  
  35. }  
  36.   
  37. Node *Connect(Node *root){  
  38.     Node *head,*p,*pre;  
  39.     head = root;  
  40.     while(1){  
  41.         p = head;  
  42.         while(p){  
  43.             if(p->left){  
  44.                 pre = head = p->left;  
  45.                 break;  
  46.             }else if(p->right){  
  47.                 pre = head = p->right;  
  48.                 break;  
  49.             }else  
  50.                 p = p->sibling;    
  51.         }  
  52.         if(!p)  
  53.             break;  
  54.         while(p){  
  55.             if(p->left){  
  56.                 pre->sibling = p->left;  
  57.                 pre = p->left;  
  58.             }  
  59.             if(p->right){  
  60.                 pre->sibling = p->right;  
  61.                 pre = p->right;  
  62.             }  
  63.             p = p->sibling;  
  64.         }  
  65.     }  
  66.     return head;  
  67. }  
  68.   
  69. int main(int argc, char const *argv[]){  
  70.     Node *root;  
  71.     createTree(root);  
  72.     printTree(root);  
  73.     cout << endl;  
  74.     Connect(root);  
  75.     return 0;  
  76. }  



[cpp]  view plain copy
  1. <pre class="cpp" name="code">#include <stdio.h>  
  2. #include <string.h>  
  3. int ans[100][100];  
  4. void print(int n){  
  5.     int count = 1,i=0,j= 0;  
  6.     ans[0][0] = 1;  
  7.     ans[n - 1][n - 1] = n * n;  
  8.     while(count < n * n - 1){  
  9.         if(i == 0 && j < n-1 ){  
  10.             ans[i][++j] = ++count;  
  11.             while(j >= 1)  
  12.                 ans[++i][--j] = ++count;  
  13.         }  
  14.         if(j == 0 && i < n-1 ){  
  15.             ans[++i][j] = ++count;  
  16.             while(i >= 1)  
  17.                 ans[--i][++j] = ++count;  
  18.         }  
  19.         if(i == n - 1 && j < n-1){  
  20.             ans[i][++j] = ++count;  
  21.             while(j < n - 1)  
  22.                 ans[--i][++j] = ++count;  
  23.   
  24.         }  
  25.         if(j == n - 1 && i < n - 1){  
  26.             ans[++i][j] = ++count;  
  27.             while(i < n - 1)  
  28.                 ans[++i][--j] = ++count;  
  29.         }  
  30.     }  
  31. }  
  32.   
  33. int main(){  
  34.     int n;  
  35.     while(scanf("%d",&n) != EOF){  
  36.         memset(ans,0,sizeof(ans));  
  37.         print(n);  
  38.         int i,j;  
  39.         for(i = 0;i < n;i++){  
  40.             for(j = 0;j < n;j++)  
  41.                 printf("%2d ",ans[i][j]);  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     return 1;  
  46. }</pre><br>  


[cpp]  view plain copy
  1. #include <stdio.h>  
  2.   
  3. int main(){  
  4.     int n;  
  5.     while(scanf("%d",&n) != EOF){  
  6.         for(int i = 0;i < n;i++){  
  7.             for(int j = 0;j < n;j++){  
  8.                int m = i + j + 1; //   
  9.                int min,max,ans = 0;  
  10.                if(m > n){  
  11.                  min = n * n - (2 * n - m + 1) * (2 * n - m) / 2 + 1;  
  12.                  max = n * n - (2 * n - m - 1) * (2 * n - m) / 2;  
  13.                  if(m % 2)  
  14.                     ans = max - (n - 1 - j);  
  15.                  else  
  16.                     ans = min + (n - 1 - j);  
  17.                }else{  
  18.                 min = m * (m - 1) / 2 + 1;  
  19.                 max = m * (m + 1) / 2;  
  20.                 if(m % 2)  
  21.                     ans = max - i;  
  22.                 else  
  23.                     ans = min + i;  
  24.   
  25.                }  
  26.                 printf("%3d ",ans);  
  27.            }  
  28.            printf("\n");  
  29.        }  
  30.     }  
  31. }  







[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <cstring>  
  4. #include <vector>  
  5. #include <algorithm>  
  6. using namespace std;  
  7.   
  8. struct Node{  
  9.     Node *next;  
  10.     int value;  
  11. };  
  12.   
  13. void createList(Node *&head){  
  14.     int x;  
  15.     Node *p,*s;  
  16.     head = new Node;  
  17.     head->next = NULL;  
  18.     cin >> x;  
  19.     while(x > 0){  
  20.         s = new Node;  
  21.         s->next = NULL;  
  22.         s->value = x;  
  23.         if(head->next == NULL)  
  24.             head->next = s;  
  25.         else  
  26.             p->next = s;  
  27.         p = s;  
  28.         cin >> x;  
  29.     }  
  30. }  
  31.   
  32. void printList(Node *head){  
  33.     Node *p = head->next;  
  34.     while(p){  
  35.         cout << p->value << " ";  
  36.         p = p->next;  
  37.     }  
  38.     cout << endl;  
  39. }  
  40.   
  41.   
  42.   
  43. void mergeTwo(Node *&A,Node* B){  
  44.     Node *p,*pre,*q,*r;  
  45.     pre = A;  
  46.     p = A->next;  
  47.     r = q = B->next;  
  48.     while(p && q){  
  49.         if(p->value < q->value){  
  50.             pre = pre->next;  
  51.             p = p->next;  
  52.         }else{  
  53.             q = q->next;  
  54.             r->next = pre->next;  
  55.             pre->next = r;  
  56.             pre = pre->next;  
  57.             r = q;  
  58.         }  
  59.     }  
  60.     if(q){  
  61.         pre->next = q;  
  62.     }  
  63. }  
  64. Node *merge(int n,Node* lists[]){  
  65.     for(int i = 1;i < n;i++){  
  66.         mergeTwo(lists[0],lists[i]);  
  67.     }  
  68.     return lists[0];  
  69. }  
  70.   
  71. int main(int argc, char const *argv[]){  
  72.     Node* lists[5];  
  73.     for(int i = 0;i < 5;i++)  
  74.         createList(lists[i]);  
  75.     merge(5,lists);  
  76.     printList(lists[0]);  
  77.     system("pause");  
  78.     return 0;  
  79. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值