有问题求教,我写的差不多了,可以cl, 但是出现的都是0 怎么办!
c++吧
全部回复
仅看楼主
level 1
gzc4188 楼主
#include <iostream>
#include <iomanip>
using namespace std;
int n;
int movecount;
const int STACKSIZE = 20;
int stack1[STACKSIZE];
int stack2[STACKSIZE];
int stack3[STACKSIZE];
int size1 = 0, size2 = 0, size3 = 0;
//17
void push(int which, int value)      {
     if(which == 1)
         stack1[size1++] = value;
     else if( which == 2)
         stack2[size2++] = value;
     else(which == 3);
         stack3[size3++] = value;
}
//26
int pop(int which)      {
     if(which==1)
         return stack1[--size1];
     else if(which==2)
         return stack2[--size2];
     else(which==3);
         return stack3[--size3];
}
void initstacks(int which)      {
     memset(stack1, size1 = 0, sizeof(stack1));
     memset(stack2, size2 = 0, sizeof(stack2));
     memset(stack3, size3 = 0, sizeof(stack3));
}
void printstack(int which)      {
     if(which == 1)
         for(int i=0; i<size1; i++)
             cout << stack1[i]<< endl;
     else if(which == 2)
         for(int i=0; i<size2; i++)
             cout << stack2[i]<< endl;
     else(which == 3);
         for(int i=0; i<size3; i++)
             cout << stack3[i]<< endl;
}
void printstacks(int which) {
     printstack(1);
     printstack(2);
     printstack(3);
}
void move(int from, int to)      {
     int i=pop(from);
     push(to,i);
}
void solve(int n ,int a, int b, int c)      {
      if (n==1) move(a,b);
      else      {
          solve(n-1,a,c,b);
          solve(1,a,b,c);
          solve(n-1,c,b,a);
      }
}
int main(void){
      cout << "Enter n: ";
      cin >> n;
      initstacks(0);
      printstacks(0);
      solve(n,1,2,3);
      printstacks(0);
      return 0;
}
2011年05月25日 06点05分 1
level 1
void push(int which, int value)       {
      if(which == 1)
          stack1[size1++] = value;
      else if( which == 2)
          stack2[size2++] = value;
      else(which == 3);
          stack3[size3++] = value;
}
下面这句有问题 else后面不能带条件,而且还多了一个分号
else(which == 3);
          stack3[size3++] = value;
这也是else(which == 3);
          for(int i=0; i<size3; i++)
              cout << stack3[i]<< endl;

2011年05月25日 07点05分 2
level 1
gzc4188 楼主
main(void)后面的那几个括号你会吗?
2011年05月25日 07点05分 3
level 1
void solve(int n ,int a, int b, int c)函数也写错了  
数据根本没有存储到下面的数组中,当然不会有值输出
int stack1[STACKSIZE];
int stack2[STACKSIZE];
int stack3[STACKSIZE];
2011年05月25日 07点05分 4
level 1
gzc4188 楼主
要怎样改呢 请教
2011年05月25日 07点05分 5
1