《数据结构与算法分析》习题-----第一章

PS:13题和14题,我将它们综合在一起·····因为我只看了题目对Object、Comparable没有了解(第一章都是一些回顾)·······

 1 //1.5编写一个递归方法,它返回数N二进制表示1的个数。

 2  #include<iostream>

 3  using namespace std;

 4  int ones(int x)

 5  {

 6      if(x < 2)

 7      return x;

 8      else

 9      return x % 2 + ones(x / 2);

10  }

11  

12  int oness(int x)

13  {

14      int n = 0;

15      while(x >= 2)

16      {

17          n += x % 2;

18          x /= 2;

19      }

20      n += x;

21      return n;

22  }

23  

24  int main( )

25  {

26      int X;

27      while(cin >> X)

28      {

29          cout << ones(X) << endl;

30          cout << oness(X) << endl;

31      }

32      return 0;

33  }

 

  1 /*1.13 题目:设计一个类模板Collection,来存储Object对象的集合(在一个数组中),以及该集合当前的大小。提供public型的函数isEmpty、makeEmpty、

  2  insert、remove和contains。当且仅当该集合中存在等于x的一个Object时,contains(x)返回true*/

  3  /*1.14 题目:设计一个类模板Comparable的对象的集合(在一个数组中),以及该集合当前的大小。提供public型的函数isEmpty、makeEmpty、remove、

  4  findMin和findMax。findMin和findMax分别返回该集合中的最小和最大的Comparable对象。*/

  5  #include<iostream>

  6  #include<cstring>

  7  using namespace std;

  8  

  9  const int M = 1000000;

 10  

 11  template <typename T>

 12  class Collection

 13  {

 14  public:

 15      Collection()

 16      {

 17          memset(a, 0, sizeof(a));

 18          count = 0;

 19      }

 20      ~Collection()

 21      {

 22          delete [] a;

 23          count = 0;

 24      }

 25      void isEmpty()

 26      {

 27          if(count)

 28              cout << "It is not empty!" << endl;

 29          else

 30              cout << "It is empty!" << endl;

 31      }

 32      void makeEmpty()

 33      {

 34          memset(a, 0, sizeof(a));

 35          count = 0;

 36      }

 37      void insert(const T &x)

 38      {

 39          a[count] = x;

 40          ++count;

 41      }

 42      void remove(const T &x)

 43      {

 44          int i;

 45          for(i = 0; i < count; ++i)

 46          {

 47              if(a[i] == x)

 48                  break;

 49          }

 50          for(; i <count; ++i)

 51          {

 52              a[i] = a[i+1];

 53          }

 54          --count;

 55      }

 56      bool contains(const T &x)

 57      {

 58          int i;

 59          for(i = 0; i <= count; ++i)

 60          {

 61              if(a[i] == x)

 62                  return true;

 63          }

 64          return false;

 65      }

 66      void findMin()

 67      {

 68          if(count == 0)

 69          cout << "The collection is empty!" << endl;

 70          else

 71          {

 72              T min = a[0];

 73              for(int i = 1; i < count; ++i)

 74              {

 75                  if(a[i] < min)

 76                  min = a[i];

 77              }

 78              cout << "The minimumest element in the collection is " << min << " !" << endl;

 79          }

 80      }

 81      void findMax()

 82      {

 83          if(count == 0)

 84          cout << "The collection is empty!" << endl;

 85          else

 86          {

 87              T max = a[0];

 88              for(int i = 1; i < count; ++i)

 89              {

 90                  if(a[i] > max)

 91                  max = a[i];

 92              }

 93              cout << "The maximumest element in the collection is " << max << " !" << endl;

 94           }

 95      }

 96  private:

 97      T a[10000];

 98      int count;

 99  };

100  

101  int main()

102  {

103      Collection<int> p;

104      int N, i, m;

105      while(cin >> N)

106      {

107          p.isEmpty();

108          for(i = 1; i <= N; ++i)

109          {

110              cin >> m;

111              p.insert(m);

112          }

113          p.isEmpty();

114          p.findMin();

115          p.findMax();

116          cout << "Input the  element you want to find:" << endl;

117          cin >> m;

118          if(p.contains(m))

119          {

120              cout << "The element exist in the collection!" << endl;

121          }

122          else

123              cout << "The element does not exist in the collection!" << endl;

124          cout << "Input the element you want to remove:" << endl;

125          cin >> m;

126          if(p.contains(m))

127          {

128              p.remove(m);

129              if(p.contains(m))

130                  cout << "The element remove unsuccessfully!" << endl;

131              else

132                  cout << "The element remove successfully!" << endl;

133          }

134          else

135          cout << "The element does not exist in the collection!" << endl;

136          cout << "Make the collection empty!" << endl;

137          p.makeEmpty();

138          p.isEmpty();

139      }

140      return 0;

141  }

 

 

你可能感兴趣的:(数据结构与算法)