- 论坛徽章:
- 0
|
曾经写过,P(m,n)和C(m,n)的程序,现在我一时半刻看不懂了:
stack可以自己写,也可以用stl里的,再改一下这个程序
- void Pmn(int m, int n){ // m >= n
- int* visited = new int[m];
- int s=-1, next, i;
- for (i=0;i<n;i++) visited[i]=0;
- stack<int> st(n);
- while(1){
- next=-1;
- for(i=s+1;i<m;i++)
- if(visited[i]==0){
- next=i;
- break;
- }
- if(next ==-1){
- if(st.IsEmpty()) break;
- s=st.pop();
- visited[s]=0;
- }else{
- st.push(next);
- visited[next]=1;
- s=-1;
- if(st.IsFull()){
- //I have got it in the stack st
- s=st.pop();
- visited[s]=0;
- }
- }
- }
- delete[] visited;
- };
- void Cmn (int m, int n){ // m >= n
- stack<int> st(n);
- int next, last=-1;
- while(1){
- next=++last;
- if(next==m)
- last=st.pop();
- else{
- st.push(next);
- if(st.IsFull()){
- //I have got it in the stack st
- if(st[0]= m-n)
- break;
- last=st.pop();
- }
- }
- }
- };
复制代码 |
|