再度图书馆
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了再度图书馆相关的知识,希望对你有一定的参考价值。
/* 二叉树小练习 子数组 8皇后92种情况 */ #include<bits/stdc++.h> using namespace std; #include <iostream> using namespace std; typedef struct node { int x; node*lc; node*rc; node(){} node(int xx){x=xx;lc=NULL;rc=NULL;} }*BiTree; int ss[]={4,2,1,0,0,3,0,0,6,5,0,0,7,0,0};int si=0; void Creat(BiTree &T) { int d=ss[si++]; if(d==0) T=NULL; else{ T=new node(d); Creat(T->lc); Creat(T->rc); } } void print(node *root,int base) { if(root) { print(root->rc,base+1); int x=base;while(x--)cout<<"*";cout<<root->x<<endl; print(root->lc,base+1); } } int sum=0; bool flag=false; void maxsum(node *root) { if(root==NULL)return; if(root->lc){maxsum(root->lc);root->x += root->lc->x;} if(root->rc){maxsum(root->rc);root->x += root->rc->x;} if(flag) {if(root->x > sum )sum=root->x;} else {sum=root->x;flag=true;} } int sonTree(node *Tb,node *T) { if(Tb) { if(Tb==T)return 1; return sonTree(Tb->lc,T)+sonTree(Tb->rc,T); } else return 0; } node*pre=NULL; node*cur=NULL; void BiTreeToLinklist(node* T) { if(T != NULL) // 双向链表化 { BiTreeToLinklist(T->lc); T->lc = pre; if(pre != NULL) { pre->rc = T; } pre = T; BiTreeToLinklist(T->rc); } } int main() { cout<<"-------- test 1---------------"<<endl; BiTree T; Creat(T); print(T,4); return 0; cout<<"-------- test 2 Tb---------------"<<endl; node* p=new node(); pre=p; BiTreeToLinklist(T); p = p->rc; while(p != NULL) { cout<<p->x<<" "; p = p->rc; } /* node* p=pre; BiTreeToLinklist(T); while(pre->lc != NULL) pre = pre->lc; while(pre != NULL) { cout<<pre->x<<" "; pre = pre->rc; } */ cout<<endl; //maxsum(T); //cout<<"sum = "<<sum<<endl; //print(T,4); return 0; } #include<bits/stdc++.h> using namespace std; void Combination1(int *arr,int *brr,int n,int k) { if(k == n) { for(int i=0;i < n;++i) { if(brr[i] == 1) { cout<<arr[i]<<" "; } } cout<<endl; } else { brr[k] = 1; Combination1(arr,brr,n,k+1); brr[k] = 0; Combination1(arr,brr,n,k+1); } } void Combination(int *arr,int *brr,int n,int k) { while(k >= 0) { if(k == n) { for(int i=0;i < n;++i) { if(brr[i] == 1) cout<<arr[i]<<" "; } cout<<endl; --k; while(brr[k] == 0) brr[--k] == 0; } else if(brr[k] == 0) brr[k++] = 1; else brr[k++] = 0; } } void Combination(int *arr,int n) { if(arr == NULL) return ; int *brr = new int[n]; memset(brr,0,sizeof(int)*n); Combination(arr,brr,n,0); } int main() { int arr[3]={1,2,3}; Combination(arr,3); return 0; } #include<bits/stdc++.h> #include<iostream> using namespace std; #include<cstring> int abs(int n) { return n<0?-n:n; } bool place(int* x,int k) { for(int i=0;i < k;++i) { if(x[i] == x[k] || abs(x[i]-x[k]) == k-i) { return false; } } return true; } void nQueue(int n,int *x,int k,int &sum) { if(k == n) { for(int i=0;i < n;++i) { for(int j=0;j < n;++j) { if(x[i] == j) { cout<<"Q"; } cout<<"#"; } cout<<endl; } cout<<endl; ++sum; } else { for(int i=0;i < n;++i) { x[k] = i; if(place(x,k)) { nQueue(n,x,k+1,sum); } } } } int nQueue(int n) { int sum = 0; int *x = new int[n]; memset(x,0,sizeof(int)*n); nQueue(n,x,0,sum); return sum; } #define n 4 int a[n][n]; void array(int cc) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(cc==0) a[i][j]=0; else cout<<a[i][j]<<" "; } if(cc!=0)cout<<endl; } } struct point { int x,y; point(){}; point(int xx,int yy){x=xx;y=yy;} }; point d[n]; int gs=0; int check(int i,int j) { for(int k=0;k<n;k++) { if(gs==0)return 1; if(abs(i-d[k].x)==0||abs(j-d[k].y)==0) return 0; if(abs(i-d[k].x)==abs(j-d[k].y)||abs(i-d[k].x)==abs(j-d[k].y)) return 0; if(d[k].x==0&&d[k].y==0) break; } a[i][j]=1; d[gs].x=i;d[gs].y=j; } void fun() { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(check(i,j)){ d[gs].x=i;d[gs].y=j;gs++;} } } } int main() { array(0); fun(); array(1); int xx; // cin>>xx; //cout<<nQueue(xx)<<endl; return 0; }
以上是关于再度图书馆的主要内容,如果未能解决你的问题,请参考以下文章