Question:// give you an array, a[]={1,1,1,1,1,2,2,2,2,3,3,3,4,5,6} //The result to be output is 1,2,3,4,5,6. ( Eliminate repetitive numbers, requiring time-space considerations).
#include <iostream>using namespace Std;template<int _n>class bitset{public:bitset () {_tidy ();//Call _Tidy () The function initializes all bits to 0.} void Show ()//displays 01 bits. {for (int _i=0;_i<_n;_i++) {cout<< (Test (_i)? ') 1 ': ' 0 ');}} BOOL Test (int _p)//tests whether the location is true (1). {int _i = _p/32;return (a[_i]>> (_p%32)) &0x1;} void _tidy () {for (int _i=0;_i<_w;_i++) {a[_i] = 0;}} void set (int _p)//sets the _p subscript to 1. {int _i = _P/32; A[_i] |= (0x1<< (_p%32));} void printf () {for (int _i=0;_i<_n;_i++) {test (_i) && (cout<<_i<< " ");}} Prints the result of the arrangement. Private:typedef Long long;//is a unit array of 4 bytes long. enum{_nw= (_n-1)/(sizeof (long) *8), _w=_nw+1};int a[_w];}; int main () {bitset<600> A;int b[]={ 1,1,1,1,2,2,3,3,34,2,423,42,34,23,1,1,3,213,123,21,3,3,4,32,543,5,46,3,45,35,23,42,3};for (int i=0;i<33;i++) { A.set (B[i]);} A.printf ();}
There are two kinds of rough ways.
#include <iostream> using namespace std; struct Node {int data; Node *next; Node ():d ata ( -1), Next (NULL) {}}; Time complexity is greatly reduced, but a certain degree of space complexity is added. Class Hash {public:hash (int a[],int n) {nodetable = new node[7]; for (int i=0;i<n;i++) {int index = Hashindex (A[i]),//search for the subscript for A[i], and in the hash table to find if the value exists, we are inserting it as a list. The nodetable[index].data=1;//is initialized to 1, indicating that there is no value below the subscript, and if the index equals the subscript, mark the subscript as 1, indicating that something is in store! node* p = nodetable[index].next; while (P!=null && p->data!=a[i]) {p=p->next; } if (P==null) {node *s = new Node (); S->data = A[i]; Nodetable[index].next=s; }}}} void Show () {for (int i=0;i<7;i++) {if (nodetable[i].data! =-1) {Node *p = NodetaBle[i].next; while (p!=null) {cout<< "" <<p->data<< ""; p = p->next; }}} cout<<endl; } int Hashindex (int x) {return x%7; } Private:node *nodetable; }; int main () {int a[]={1,2,2,1,1,1,3,4,5}; Hash sh (a,9); Sh. Show (); return 0; }--------------------------------------------------------------#include <iostream> #include <stdlib.h& Gt using namespace Std; Rough Cut method, time complexity is slightly higher, space complexity is lower. void grial (int a[],int& n)//n is passed in by reference, because the size of the array will change dynamically when I delete a repeating value, and I don't need to record the actual value of N. {int i=0; Int J; int k; for (;i<n;) {for (j=i+1;j<=n;j++) {if (A[i]==a[j]) {for (k=j;k <n;k++) {a[k]=a[k+1];//Delete the duplicate value with A[i] and subtract n by 1; } n--; } if (j>=n) {i++; }} if (A[i]==a[i+1]) {for (k=i;k<n;k++) { A[K]=A[K+1]; } n--; }}} int main () {int a[100]; for (int i=0;i<100;i++) {a[i]=rand ()%100; } int b=99; Grial (A, b); for (int i=0;i<b;i++) {cout<<a[i]<<endl; } return 0; }
C + + array operations (Repeat, sort, bitset)