1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- //
- // Created by tangs on 2018/9/19.
- //
- #include <iostream>
- #include <time.h>
- #include <algorithm>
- using namespace std;
- struct Digit {
- int *Ary;
- int Length;
- Digit(int length) {
- int *tempAry = new int[length];
- this->Length = 2*length;
- this->Ary = new int[this->Length];
- for (int i = 0; i < length;i ++){
- tempAry[i] = rand();
- }
- for (int i =0 ;i < length;i ++){
- this->Ary[i] = tempAry[i];
- this->Ary[length + i] = tempAry[i];
- }
- this->Sort();
- }
- ~Digit() {
- delete(this->Ary);
- this->Length = 0;
- }
- void Sort();
- void Print();
- void DelRepeat();
- };
- void Digit::Print() {
- cout << "Num " << this->Length << endl;
- for (int i = 0; i < this->Length; i++) {
- cout << this->Ary[i];
- if (i == this->Length - 1) {
- cout << endl;
- } else {
- cout << " ";
- }
- }
- }
- /**
- * 将顺序表排序生成有序顺序表
- */
- void Digit::Sort() {
- sort(this->Ary,this->Ary + this->Length);
- }
- /**
- * 删除有序顺序表中重复的元素
- * @return
- */
- void Digit::DelRepeat() {
- if (this->Length < 1) {
- return;
- }
- int temp = this->Ary[0];
- int k = 1;
- for (int i = 1; i < this->Length; i++) {
- if (this->Ary[i] != temp) {
- temp = this->Ary[i];
- this->Ary[k] = this->Ary[i];
- k++;
- }
- }
- this->Length = k;
- }
- /**
- * 从有序表中删除所有其值重复的元素,使表中所有元素的值均不同
- * @return
- */
- int main(){
- srand((unsigned)time(NULL));
- Digit digit(10);
- digit.Print();
- digit.DelRepeat();
- digit.Print();
- }
|