voltage

vertexes

Sep 19th, 2011
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. /*
  2.  * @author voltage
  3.  */
  4. #include <stdio.h>
  5. #include <math.h>
  6.  
  7. typedef struct arr_2d{
  8.     private:
  9.         int *mh;
  10.         int n,m;
  11.     public:
  12.         arr_2d(int n, int m){
  13.             this->n = n; this->m = m;
  14.             this->mh = new int[n*m];
  15.         };
  16.         arr_2d(const arr_2d &a){
  17.             this->n = a.n; this->m = a.m;
  18.             this->mh = new int[n*m];
  19.             for(int i=0; i<n*m; i++)
  20.                 this->mh[i] = a.mh[i];
  21.         }
  22.         ~arr_2d(){
  23.             delete this->mh;
  24.         };
  25.         int &e(int i, int j){
  26.             return mh[i*m + j];
  27.         }
  28.         int h(){ return this->n; }
  29.         int w(){ return this->m; }
  30. } vert_mx;
  31.  
  32. vert_mx c(int d){
  33.     if(d==0){
  34.         vert_mx m(1,1);
  35.         m.e(0,0) = 0;
  36.         return m;
  37.     }
  38.     vert_mx p = c(d-1);
  39.     bool o = (d%2) > 0;
  40.     int v = o ? p.h() * 2 : p.h();
  41.     int h = o ? p.w() : p.w() * 2;
  42.     vert_mx m(v,h);
  43.     for(int l=0; l<p.h(); l++)
  44.         for(int k=0; k<p.w(); k++)
  45.             m.e(l,k) = p.e(l,k);
  46.     if(o){
  47.         for(int i=v-1; i>=p.h(); i--)
  48.             for(int j=0; j<p.w(); j++)
  49.                 m.e(i,j) = m.e(v-i-1,j) + int(pow(2,d-1));
  50.     }else{
  51.         for(int i=0; i<p.h(); i++)
  52.             for(int j=h-1; j>=p.w(); j--)
  53.                 m.e(i,j) = m.e(i,h-j-1) + int(pow(2,d-1));
  54.     }
  55.     return m;
  56. }
  57.  
  58. int main(){
  59.     int n;
  60.     printf("n = ");
  61.     scanf("%d",&n);
  62.     vert_mx m = c(n);
  63.     for(int i=0; i<m.h(); i++){
  64.         for(int j=0; j<m.w(); j++)
  65.             printf("%-5d ",m.e(i,j));
  66.     putchar('\n');
  67.     }
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment