- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <vector>
- #include <algorithm>
- using namespace std;
- struct Node{
- Node *left;
- Node *right;
- Node * sibling;
- int value;
- };
- void createTree(Node *&root){
- int x;
- cin >> x;
- if(x == 0)
- root = NULL;
- else{
- root = new Node;
- root->sibling = NULL;
- root->value = x;
- createTree(root->left);
- createTree(root->right);
- }
- }
- void printTree(Node *root){
- if(root){
- cout << root->value << " ";
- printTree(root->left);
- printTree(root->right);
- }
- }
- Node *Connect(Node *root){
- Node *head,*p,*pre;
- head = root;
- while(1){
- p = head;
- while(p){
- if(p->left){
- pre = head = p->left;
- break;
- }else if(p->right){
- pre = head = p->right;
- break;
- }else
- p = p->sibling;
- }
- if(!p)
- break;
- while(p){
- if(p->left){
- pre->sibling = p->left;
- pre = p->left;
- }
- if(p->right){
- pre->sibling = p->right;
- pre = p->right;
- }
- p = p->sibling;
- }
- }
- return head;
- }
- int main(int argc, char const *argv[]){
- Node *root;
- createTree(root);
- printTree(root);
- cout << endl;
- Connect(root);
- return 0;
- }
- <pre class="cpp" name="code">#include <stdio.h>
- #include <string.h>
- int ans[100][100];
- void print(int n){
- int count = 1,i=0,j= 0;
- ans[0][0] = 1;
- ans[n - 1][n - 1] = n * n;
- while(count < n * n - 1){
- if(i == 0 && j < n-1 ){
- ans[i][++j] = ++count;
- while(j >= 1)
- ans[++i][--j] = ++count;
- }
- if(j == 0 && i < n-1 ){
- ans[++i][j] = ++count;
- while(i >= 1)
- ans[--i][++j] = ++count;
- }
- if(i == n - 1 && j < n-1){
- ans[i][++j] = ++count;
- while(j < n - 1)
- ans[--i][++j] = ++count;
- }
- if(j == n - 1 && i < n - 1){
- ans[++i][j] = ++count;
- while(i < n - 1)
- ans[++i][--j] = ++count;
- }
- }
- }
- int main(){
- int n;
- while(scanf("%d",&n) != EOF){
- memset(ans,0,sizeof(ans));
- print(n);
- int i,j;
- for(i = 0;i < n;i++){
- for(j = 0;j < n;j++)
- printf("%2d ",ans[i][j]);
- printf("\n");
- }
- }
- return 1;
- }</pre><br>
- #include <stdio.h>
- int main(){
- int n;
- while(scanf("%d",&n) != EOF){
- for(int i = 0;i < n;i++){
- for(int j = 0;j < n;j++){
- int m = i + j + 1; //
- int min,max,ans = 0;
- if(m > n){
- min = n * n - (2 * n - m + 1) * (2 * n - m) / 2 + 1;
- max = n * n - (2 * n - m - 1) * (2 * n - m) / 2;
- if(m % 2)
- ans = max - (n - 1 - j);
- else
- ans = min + (n - 1 - j);
- }else{
- min = m * (m - 1) / 2 + 1;
- max = m * (m + 1) / 2;
- if(m % 2)
- ans = max - i;
- else
- ans = min + i;
- }
- printf("%3d ",ans);
- }
- printf("\n");
- }
- }
- }
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <vector>
- #include <algorithm>
- using namespace std;
- struct Node{
- Node *next;
- int value;
- };
- void createList(Node *&head){
- int x;
- Node *p,*s;
- head = new Node;
- head->next = NULL;
- cin >> x;
- while(x > 0){
- s = new Node;
- s->next = NULL;
- s->value = x;
- if(head->next == NULL)
- head->next = s;
- else
- p->next = s;
- p = s;
- cin >> x;
- }
- }
- void printList(Node *head){
- Node *p = head->next;
- while(p){
- cout << p->value << " ";
- p = p->next;
- }
- cout << endl;
- }
- void mergeTwo(Node *&A,Node* B){
- Node *p,*pre,*q,*r;
- pre = A;
- p = A->next;
- r = q = B->next;
- while(p && q){
- if(p->value < q->value){
- pre = pre->next;
- p = p->next;
- }else{
- q = q->next;
- r->next = pre->next;
- pre->next = r;
- pre = pre->next;
- r = q;
- }
- }
- if(q){
- pre->next = q;
- }
- }
- Node *merge(int n,Node* lists[]){
- for(int i = 1;i < n;i++){
- mergeTwo(lists[0],lists[i]);
- }
- return lists[0];
- }
- int main(int argc, char const *argv[]){
- Node* lists[5];
- for(int i = 0;i < 5;i++)
- createList(lists[i]);
- merge(5,lists);
- printList(lists[0]);
- system("pause");
- return 0;
- }