It's easy not to worry too much about efficiency, although we have to reorder each time
Note the use of vectors is very convenient.
Another trick is to use an existing bool array to record whether or not it exists.
#include <iostream>#include<vector>#include<algorithm>using namespacestd;BOOLhave[ +] = {0};vector<int> v;//using vectors to realize the simplicity of dynamic arraysBOOLCmp_int (Const int& A,Const int&b) { returna<b;}intMainintargcChar Const*argv[]) { intN; CIN>>N; for(inti =0; I < n; ++i) {intK; CIN>>K; V.push_back (k); HAVE[K]=true; } while(1){ BOOLHave_new =false;//set temporary check variables to see if this scan has new elements .//reorder after each updatesort (V.begin (), V.end (), cmp_int); N= V.size ();//use N to determine the scope of this scan if the loop is directly using size to produce an iterative effect. for(inti =1; I < n; ++i) { for(intj=0; J < I; ++J) {//Enumerate two numbers intres = V[i]/V[j]; if(!have[res]) {//Judging if it's already in the vector.V.push_back (RES); Have[res]=true; Have_new=true; } } } if(!have_new) { Break; }} cout<<v.size () <<Endl; return 0;}
"Algorithmic Learning Notes" 68. Enumerating SJTU OJ 1272 writing games