maze:
#include enum boolean {false, true}; template struct node {t val; node * next;}; template class list {node * head; int size; Public: list () {head = null; size = 0;} Virtual Boolean INSERT (T Val); Boolean Empty (); Boolean delete (t val); Boolean Contains (T Val); void print (); ~ List );}; template list :: ~ List () {node * temp; for (Node * P = head; p;) {temp = p; p = P- > next; delete temp;}}
Template Boolean List :: Empty () {if (Head) Return False; Else Return True;} Template Boolean List :: Insert (T x) {Node * node = new node ; if (node) {node-> val = x; node-> next = head; head = node; size ; return true;} Return False;
Template Boolean List :: Delete (T x) {node * TEMP; if (Empty ()) Return False; if (Head-> Val == x) {TEMP = Head-> NEXT; delete head; size ---; head = Temp; return true;} for (Node * P = Temp = head; p; temp = p, p = p-> next) IF (p-> val = = x) {temp-> next = p-> next; delete p; size ---; return true;} return false;
Template Boolean List :: Contains (T x) {for (Node * P = Head; P; P = P-> Next) f (p-> val == x) Return True Return False;}
Template Void List :: Print () {for (Node * P = HEAD; P; P = P-> Next) Cout << P-> Val << "; COUT < <"/ n";} void main () {list intlist; int 1; for (i = 0; i <10; i ) Intlist.insert (i); intlist.print (); intlist.delete 1); Intlist.delete (2); intlist.print ();
List floatlist; floatlist.insert (3.1); floatlist.insert (1.5); floatlist.print ();
List charlist; charlist.insert ("Program."); Charlist.insert ("My DS"); Charlist.insert ("IS"); charlist.insert ("this"); charlist.print () }
#include #include Enum boolean {false, true}; const Int maxSize = 20; struct celltype {int ID; celltype * next;}; struct queue {celltype * front, * { }; struct Edgenode {Int Adjvex; Edgenode * Next;}; struct vexNode {Int info; ednode * next;}; vexNode graph [maxsize]; int N;
Void Makenull (Queue * HEAD) {
Head-> front-> next = null; head-> rest = head-> front;} void createqueue (queue * head) {head-> front = (celltype *) malloc (cellType); head-> r = (cellType *) malloc (celltype); makeenull (hEAD);} Boolean ISempty (Queue * Head) {if (head-> front == head-> rear) return true; Else Return false;} void dequeue (Queue * head) {CELLTYPE * T; if (! iSempty (Head)) {t = head-> front; head-> front = head-> front-> next; free (} else printf) s "," deleteError! "); -> Next; Head-> Rear-> ID = ID;
Void creategraph () {INT I, K; Edgenode * P; Printf ("% s", "please input max no ="); scanf ("% d", & n); for (i = 0; i = 0) {graph [i] .next = (edgenode *) malloc (singleof (edgenode)); p = graph [i] .next; p-> adjvex = k; p-> next = NULL;} While (k> = 0) {scanf ("% D", & k); if (k> = 0) {p-> next = (edgenode *) malloc (singleof (edgenode)); p = P -> Next; P-> Adjvex = k;}} P-> next = null;}} void print () {INT i; edgenode * p; for (i = 0; i adjvex); Printf ("/ n");}} void bfs () {void search (int, boolean *); boolean visited [maxSize]; int i; for (i = 0; i front-> next; adj = graph [p-> id] .next; while (adj) {if (! Visited [adj to-> adjg) {enqueue (q, adj to> adjvex ); Printf ("% D", adj to-> adjvex); Visited [adj to-> adjgEx] = true;} adj = adj to} DEQUEUE (q);
}} void main () {creategraph (); bfs ();} This is the backtracking method of the N Queens, non-recursive implementation. This algorithm has difficulty, it is best to refer to the book. This problem is also a classic classic Example, recommend yourself to practice. Also, I posted programs to debug passes in TC3.0. # Include #include #include #include ENUM BOOL {False, True}; Class Queen {Friend Int Nqueen (int); Private: Inline Bool Place (INT K); Void Backtrack (Void); INT N, * X; Long Sum;}; Inline Bool Queen :: Place (INT K) {Int J; For (J = 1; J 0) {x [k] = 1; WHILE ((x [k] <= n) &&! (Place (k))) x [k] = 1; if (x [k] <= n) IF (k == n) SUM ; ELSE {K ; x [k] = 0;} else k -;}}
INT nqueen (int N) {queen x; xn = n; x.sum = 0; int * p = new int [n 1]; for (int i = 0; i <= n; i ) p [i] = 0; xx = p; x.backTrack (); delete [] p; return x.sum;} int main () {int N; Struct Time T1, T2; // Take the system time. GetTime (& T1); cout << "INPUT N ="; cin >> n; cout << nqueen (n) << endl; gettime (& t2); cout << (t2.ti_sec - t1.ti_sec) (t2.ti_hund-t1.ti_hund * 1.0 / 100; return 0;}
2 C programs for high-precision plus, reduction and multiplication ./* prositive highpricle- Operation of baniry system * / # include #include const Int maxsize = 20; // max length Of The NumberConst Int K = 2; // Baniry System You can modify the high-precision Class HP {INT LEN; // length of KB, {Int Len; // length; // store high precations Number public: hp () HP HP :: Operator = (HP C);}; hp :: Hp () {len = 0; MEMSET (S, 0, MaxSize * SizeOf (Int));} istream & Operator >> (ISTREAM & IN, HP & HP ) {Char S [MaxSize]; INT I; cout << "Input number ="; cin >> s; hp.len = strlen (s); for (i = 0; i = 0; I -) COUT << HP.S [I]; Return Out;} HP Operator (HP A, HP B) {INT I, LEN; HP C; IF A.LEN> B.LEN) len = a.len; else len = b.len; // Get the Bigger Length of A, B FOR (i = 0; i = K) {CS [I] - = K; CS [i 1]; // Add 1 to a higher position}}}} (cs [len]> 0) C.1 = len 1; else c.1 = le; return c;} HP Operator - (HP A, HP B) // Different of the Two HighPrecision Numbers {Int Len, i; HP C; IF A.LEN> B.LEN) LEN = a.len; else len = b.len; c.1 = 4; for (i = 0; i
// Subtract 1 to higher position}}} while (cs [len-1] == 0 &&len> 1) --Len; c.1 = le; return; hp operator * (Const HP & A, Const HP & B {INT LEN, I, J; HP C; For (i = 0; i 1 && CS [LEN-1] == 0) --Len; C. LEN = LEN; RETURN C;} HP HP :: Operator = (hp c) {INT I; LEN = C.1; for (i = 0; i > A >> B; c = a b; cout << a << ' ' << b << "=" << C << endl; c = ab; cout << a << '-' << b << "=" << c << Endl; c = a * b; cout << a << '*' << B << = "<< c << Endl; Return 0;} Joseph question (monkey chose king): N Monkey wants to choose the king, the election method is as follows: All monkeys are pressed by 1, 2, ..., n numbers Circle, starting from 1, 2, ..., m to number 1, 2, ... This monkey is the king .n and m input by the keyboard, print out the last remaining monkey number. Because of the exercises in many books, and many people ask, ... # include #include < Malloc.h> struct link {int ID; link * next;}; void initialize (int N, link * head) {INT i; link * p; for (i = 1, p = head; i <= n; i ) {P-> next = (link *) malloc (sizeof (limited)); p- = p-> next; p-> id = i;} P-> next = head-> next;}
Void delete (link * node) {link * temp; temp = node-> next; printf ("% d", node-> next-> id); Node-> Next = Temp-> Next; Free (TEMP); } void main () {link * head, * p; int N, i, k; Printf ("% s", "input n, k ="); scanf ("% D% D", & n, & k); HEAD = (link *) Malloc (Sizeof (Link)); Head-> Next = (Link *) Malloc (SIZEOF (LINK)); Initialize (n, head); for (p = head, i = 1; p! = P-> Next; P = P-> Next, i ) {if (i == k) {i = 1; delete (p);}} printf ("/ n"); Printf ("% D", P-> ID);} The following is a plurality of programs that are sequencing and selecting sorting efficiency, randomly generate 20000 data, only one sort algorithm can be selected at a time, the last output program runs: #include #include #include const Int maxSize = 20000; const INT n = 5; int data [maxSize]; void print () {INT i; for (i = 0; i < MaxSize; i ) Printf ("% D", DATA [I]);} void create () {INT i; randomize (); for (i = 0; i = first; I - pushdown_minheap (i, last); for (i = last; i> first; i -) {swap (& data [first], & data [i]); Pushdown_Minheap (First, i - 1); }}
Void sort () {INT I, J; For (i = 0; i
Void Quicksort (int DATA [], INT P, INT R {INT Q; IF (P
/ * Insertion Sort Data [] Element, Which from Start to Last Insert Sort * / Template Void Insertion_Sort (T Data [], Int Start, Int Last) {INT I, J; T Key; for (i = START 1; I = start && data [j] = data [j] = data [J] ; J-;} DATA [J 1] = key;}}
/ * Select Selection Sort: Copy Source [] Element from Start To Last to B [] by asc * / template void selection_sort (t source [], tb [], int start, int last) {INT i, J, T; t temp; for (i = start; i Source [J ]) T = J; Temp = source [t]; source [t] = source [i]; source [i] = Temp; // swap minimal element / to source [i] b [i] = source [i] // copy to b}} Newton iterative method, formula is: x (i 1) = xi - f (x) / f` (x) Tangent approach: #include # INCLUDE Float Newton (INT & A, INT & B) {const float eps = 1e-6; float x0 = 100, x = 0; float fx, flx; while (1) {fx = a * x0 * x0 - B * X0 1; FLX = 2 * a * x0 - b; x = x0 - fx / flx; if (X - X0) <= EPS) Break; Else X0 = x;} Return X;}
Void main () {float x; int A0, A1; Scanf ("% I% I", & A0, & A1); x = newton (A0, A1); Printf ("% f / n", x);}
Here, there is a two-point method, root X is between (a, b), pay attention to the correct range: 1) Take the C = (A B) / 2 of A, B), and divide the root interval between half , Which is determined which interval is in. Three situations: 2) f (c) == Accuracy, C is the root 3) IF f (c) * f (a) <0, the root range is [A, C], B = C, turn 1 4) IF f (c) * f (a)> 0, find root interval in [C, B], A = C, turn 1)
This algorithm can only find a root, solving 3 equation applications. Subtapped matrices Basic computing issues: Sproducts of the three-way group sequence tables of "line logical link information", to achieve two matrices, subtraction, Multiplier operation. The input form of the sparse matrix adopts a three-rating, and the matrix of the calculation results are listed in the usual array. Void Tsmatrix_Add (TSMatrix A, TSMATRIX B, TSMATRIX & C) // Triendou Group Representation {C.MU = A.MU; C. Nu = A.NU; C.TU = 0; PA = 1; PB = 1; PC = 1; for (x = 1; x <= a.mu; x ) // The addition of the matrix {While (A.Data [PA] .i B.Data [PB] .j) {C.Data [PC] .i = x; C. Data [PC] .j = B.Data [Pb] .j; C. Data [PC] .E = B.Data [PB] .E; PB ; PC ;} else {c.data [pc] .i = x; c.data [pc] .j = a.data [PA ] .j; C. Data [PC] .e = a.data [PA] .e PA ; PC ;}} // while while (a.data [PA] == x) // Insert a remaining elements (C. Data [PC] .i = x; C. Data [Pc] .j = a.data [PA] .j; c.data [pc] .e = a.data [PA] .e PA ; PC ;} while (b.data [pb] == x) / / Insert the remaining element (first line) {C. Data [Pc] .i = x; C. Data [Pc] .j = B.Data [Pb] .j; C. Data [PC] .e = B. Data [Pb] .E; PB ; PC ;}} // for c.tu = pc;} // tsmatrix_add
Void Tsmatrix_minus (TSMATRIX A, TSMATRIX B, TSMATRIX & C) // Triendou group represented sparse matrix subtraction {C.MU = A.MU; C. Nu = a.nu; c.tu = 0; PA = 1; PB = 1; PC = 1; for (x = 1; x <= a.mu; x ) // minimize the matrix {while (A.Data [PA] .i B.Data [Pb] .j) {C.Data [PC] .i = x; C. Data [PC] .j = B.Data [Pb] .j; C. Data [pc] .e = -b.data [pb] .E; PB ; PC ;} else {c.data [pc] .i = x; c.data [pc] .j = a.data [PA] .j; C. Data [PC] .e = a.data [PA] .e PA ; PC ;}} // while while (A.Data [PA] == x) // Insert a remaining elements ( X line) {c.data [pc] .i = x; C. Data [PC] .j = a.data [PA] .j; C.Data [PC] .e = a.data [PA]. E PA ; PC ;} while (b.data [pb] == x // Insert the remaining element (first line) {C.Data [pc] .i = x; c.data [pc] .j = b.data [pb] .j; C.Data [PC] .e; pb ; pc ;}} // for c.tu = pc;} // tsmatrix_minustypedef struct {Int i, j; int e;} triple;
TypedEf struct {triple data [401]; int RPOS [21]; int MU, NU, TU;} rlsmatrix
Void Tsmatrix_mults (Rlsmatrix A, RLSMATRIX B, RLSMATRIX & C) // Sparse Matrix Multiplication {Int Arow, Brow, Ccol, TP, P, Q, T; Int Ctemp [401]; IF (A.nu! = B.MU) {Error = true; return;} C.MU = A.MU; C. Nu = B.nu; C.TU = 0; if (a.tu * b.tu! = 0) {for (AROW = 1; AROW <= a.mu; arow ) {MEMSET (CTEMP, 0, SIZEOF (CTEMP)); C.RPOS [AROW] = C.TU 1; IF (Arow 400) Return; C.Data [C.TU] .i = arow; c.data [c.tu] .j = ccol; C. Data [C.TU] .E = CTEMP [CCOL];}}}} Several Best Algorithms and Data Structure Learning Website http://algorithm.lzu.edu.cn/http://algorithm. MYRICE.com/http://www.cpascal.com/http://gz6hs.net/lzoi/index.htmlhtp://www.itisonline.org/Http://218.5.5.86/drs/http:// 202.109.195.141/chenyan/noi/noi.htmhtp://www.periodicals.com.cn/qikan/index.j- Sphttp: //218.5.5.197/~drs/http://www.bgy.gd.cn/gdoi/oszl/index.htmhttp: //noi.stedu.net/index0.asphttp: //vip.6to23.com / DCYU / / / DCYU: http://www.cfcs.com.cn/fjas/index.htm // Information learning good site, there are many learning resources http://www.cfcs.com.cn /fjas/index.htm // Fujian Information Learning Olympics Station, there are not many content :)