main.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Created by tangs on 2018/9/19.
  3. //
  4. #include <iostream>
  5. #include <time.h>
  6. #include <algorithm>
  7. using namespace std;
  8. struct Digit {
  9. int *Ary;
  10. int Length;
  11. Digit(int length) {
  12. int *tempAry = new int[length];
  13. this->Length = 2*length;
  14. this->Ary = new int[this->Length];
  15. for (int i = 0; i < length;i ++){
  16. tempAry[i] = rand();
  17. }
  18. for (int i =0 ;i < length;i ++){
  19. this->Ary[i] = tempAry[i];
  20. this->Ary[length + i] = tempAry[i];
  21. }
  22. this->Sort();
  23. }
  24. ~Digit() {
  25. delete(this->Ary);
  26. this->Length = 0;
  27. }
  28. void Sort();
  29. void Print();
  30. void DelRepeat();
  31. };
  32. void Digit::Print() {
  33. cout << "Num " << this->Length << endl;
  34. for (int i = 0; i < this->Length; i++) {
  35. cout << this->Ary[i];
  36. if (i == this->Length - 1) {
  37. cout << endl;
  38. } else {
  39. cout << " ";
  40. }
  41. }
  42. }
  43. /**
  44. * 将顺序表排序生成有序顺序表
  45. */
  46. void Digit::Sort() {
  47. sort(this->Ary,this->Ary + this->Length);
  48. }
  49. /**
  50. * 删除有序顺序表中重复的元素
  51. * @return
  52. */
  53. void Digit::DelRepeat() {
  54. if (this->Length < 1) {
  55. return;
  56. }
  57. int temp = this->Ary[0];
  58. int k = 1;
  59. for (int i = 1; i < this->Length; i++) {
  60. if (this->Ary[i] != temp) {
  61. temp = this->Ary[i];
  62. this->Ary[k] = this->Ary[i];
  63. k++;
  64. }
  65. }
  66. this->Length = k;
  67. }
  68. /**
  69. * 从有序表中删除所有其值重复的元素,使表中所有元素的值均不同
  70. * @return
  71. */
  72. int main(){
  73. srand((unsigned)time(NULL));
  74. Digit digit(10);
  75. digit.Print();
  76. digit.DelRepeat();
  77. digit.Print();
  78. }