PAT 1098. Insertion or Heap Sort (25)

题目地址: http://www.patest.cn/contests/pat-a-practise/1098

本来试图复习一下c++的继承,  无奈都忘光了, static 成员, 抽象类, 纯虛函数。。。。

c++语法都忘光了, 一个语句: A a = new A();  竟然编译不过, 必须用指针接, 哎, A *a = new A();  接近一年没写c++, 都还给老师了

对于c++的两种声明对象:

A a; //在栈上分配空间

A *a = new A(); //在堆上

下面的代码我好像忘了写析构函数

  1 #include<iostream>

  2 #include<cstdio>

  3 #include<cstdlib>

  4 #include<cstring>

  5 #include<algorithm>

  6 

  7 using namespace std;

  8 /*

  9 试图用使用继承, 抽象函数搞不定阿

 10 struct Sortor

 11 {

 12     string name;

 13     int length;

 14     int iter;

 15     int *arr;

 16 

 17     Sortor(int *a, int n):arr(a),length(n),iter(n)

 18     {

 19 

 20     }

 21     virtual void start() = 0;

 22     virtual bool has_next() = 0;

 23     virtual void next_iteration() = 0;

 24 };

 25 */

 26 

 27 

 28 const int MAXN = 200;

 29 int nums[MAXN];

 30 int part_sorted_nums[MAXN];

 31 int n;

 32 

 33 struct HeapSortor//: public Sortor

 34 {

 35     string name;

 36     int length;

 37     int iter;

 38     int arr[MAXN];

 39     HeapSortor(int *a, int len):length(len),iter(len),

 40     name("Heap Sort")

 41     {

 42         /*这个函数也不会用了, 真坑*/

 43         //memcpy(arr + 1, a + 1, len);

 44         for (int i = 1; i <= len; ++i)

 45             arr[i] = a[i];

 46     }

 47 

 48     int parent(int i)

 49     {

 50         return i >> 1;

 51     }

 52 

 53     int left(int i)

 54     {

 55         return i << 1;

 56     }

 57 

 58     int right(int i)

 59     {

 60         return i << 1 | 1;

 61     }

 62 

 63     void maintain(int i, int len)

 64     {

 65         int l = left(i);

 66         int r = right(i);

 67         int largest = i;

 68         if (l <= len && arr[l] > arr[i]) largest = l;

 69         if (r <= len && arr[r] > arr[largest]) largest = r;

 70 

 71         if (largest != i)

 72         {

 73             swap(arr[i], arr[largest]);

 74             maintain(largest, len);

 75         }

 76     }

 77 

 78     void build_max_heap()

 79     {

 80         for (int i = (length >> 1); i > 0; --i)

 81         {

 82             maintain(i, length);

 83         }

 84     }

 85 

 86     bool has_next()

 87     {

 88         return iter > 1;

 89     }

 90 

 91     void next_iteration()

 92     {

 93         swap(arr[1], arr[iter]);

 94         maintain(1, --iter);

 95     }

 96 

 97     void start()

 98     {

 99         build_max_heap();

100         iter = length;

101     }

102 };

103 

104 struct InsertionSort

105 {

106     string name;

107     int length;

108     int iter;

109     int arr[MAXN];

110     InsertionSort(int *a, int len):length(len),iter(0),

111     name("Insertion Sort")

112     {

113        // memcpy(arr + 1, a + 1, len);

114         for (int i = 1; i <= len; ++i)

115             arr[i] = a[i];

116     }

117 

118     void start()

119     {

120         iter = 2;

121     }

122 

123     bool has_next()

124     {

125         return iter <= length;

126     }

127 

128     void next_iteration()

129     {

130         int key = arr[iter];

131         int i = iter - 1;

132         while (i > 0 && arr[i] > key)

133         {

134             arr[i + 1] = arr[i];

135             --i;

136         }

137         arr[i + 1] = key;

138         ++iter;

139     }

140 };

141 

142 

143 bool check(int *a, int *b, int len)

144 {

145     for (int i = 1; i <= n; ++i)

146     {

147         if (a[i] != b[i])

148             return false;

149     }

150     return true;

151 }

152 

153 void print(int *arr, int n)

154 {

155     for (int i = 1; i < n; ++i)

156     {

157         cout << arr[i] << " ";

158     }

159     cout << arr[n] << endl;

160 }

161 

162 /*下面两个check函数本可以利用多态合并成一个, 无奈c++语法,继承的东西早忘光了*/

163 bool check_heap_sort()

164 {

165     HeapSortor sortor(nums, n);

166     sortor.start();

167     while(sortor.has_next())

168     {

169         sortor.next_iteration();

170         if (check(sortor.arr, part_sorted_nums, n))

171         {

172             cout << "Heap Sort" << endl;

173             if (sortor.has_next())

174                 sortor.next_iteration();

175             print(sortor.arr, n);

176             return true;

177         }

178     }

179     return false;

180 }

181 

182 bool check_insertion_sort()

183 {

184     InsertionSort sortor(nums, n);

185     sortor.start();

186     while(sortor.has_next())

187     {

188         sortor.next_iteration();

189         if (check(sortor.arr, part_sorted_nums, n))

190         {

191             cout << "Insertion Sort" << endl;

192             if (sortor.has_next())

193                 sortor.next_iteration();

194             print(sortor.arr, n);

195             return true;

196         }

197     }

198     return false;

199 }

200 

201 int main()

202 {

203     while(cin >> n)

204     {

205         for (int i = 1; i <= n; ++i)

206             cin >> nums[i];

207         for (int i = 1; i <= n; ++i)

208             cin >> part_sorted_nums[i];

209         if (!check_heap_sort())

210             check_insertion_sort();

211     }

212     return 0;

213 }

 

你可能感兴趣的:(insert)