Euclidean method
Beg least common multiple
Bubble sort
1, the greatest common divisor method to seek, abbreviated to GCD. Application: the reduction fraction, which is the numerator denominator divided by greatest common divisor.
#include <iostream>#include<cstdio>using namespacestd;intgcdint,int);intMain () {cout<< gcd (6,9) << Endl;//output=3 return 0; }intgcdintMintN) { for(intR (r=m%n)! =0; M=n, n=R); returnN;}
2, least common multiple, least common multiple, abbreviated as LCM.
#include <iostream>#include<cstdio>using namespacestd;intgcdint,int);intLcmint,int);intMain () {cout<< gcd (6,9) << Endl;//output=3cout << LCM (6,9) << Endl;//output=18 return 0; }intgcdintMintN) { for(intR (r=m%n)! =0; M=n, n=R); returnN;}intLcmintMintN) {returnM*n/gcd (M, n);}
3, the number of the sort
#include <cstdio>#include<cstdlib>#include<ctime>#defineN 1000voidSwapint& A,int&b) {a= A +b; b= A-b; A= A-b;}voidSortint*arr,intLen) { for(inti =0; I! = len-1; ++i)//the array of Len elements requires a row (len-1) for(intj =0; J! = len-i-1; ++j)if(Arr[j] > arr[j+1]) Swap (arr[j], arr[j+1]);}intArr[n];intMain () {Srand (0)); for(inti =0; i < N; i++) printf ("%4d", Arr[i]=rand ()% -+1);//generates a random integer of n 1-100. printf"\ n"); Sort (arr, N); for(i =0; i < N; ++i) printf ("%4d", Arr[i]); printf ("\ n"); return 0;}
#include <cstdio>#include<cstdlib>#include<ctime>#defineN 100voidSwapint&a,int&b) {a= A +b; b= A-b; A= A-b;}voidSortint*arr,intLen) { for(inti =0; I! = Len-1; ++i) for(intj =0; J! = Len-i-1; ++j)if(Arr[j] > arr[j+1]) Swap (arr[j], arr[j+1]);}intMain () {/*int a = 3, b = 4; Swap (A, b); printf ("%d%d\n", A, b); */Srand ((int) Time (0)); intArr[n]; for(inti =0; I! = N; ++i) {printf ("%4d", arr[i] = rand ()% -+1); } printf ("\ n"); Sort (arr, N); for(i =0; I! = N; ++i) {printf ("%4d", Arr[i]); } printf ("\ n"); return 0;}
Sort objects, how C + + creates objects: http://blog.csdn.net/azhexg/article/details/14225545
#include <cstdio>#include<cstdlib>#include<ctime>#include<iostream>#include<string>using namespacestd;#defineN 10/*void Swap (int &a, int &b) {a = a + B; b = a A; A = a-B;} void sort (int *arr, int len) {for (int i = 0; I! = len-1; ++i) for (int j = 0; J! = Len-i-1; ++j) if (Arr[j] > Arr[j+1]) swap (Arr[j], arr[j+1]);}*/classStudent {friend Ostream& Print (Ostream&,Conststudent&); FriendvoidSort (Student **arr,intlen); Public: Student (Const string& Name,intscore): Name (name), score (score) {}Private: stringname; intscore;};voidSwap (student* &left, student* &Right ) {Student*temp; Temp=Left ; Left=Right ; Right=temp;}voidSort (Student **arr,intLen) { for(inti =0; I! = Len-1; ++i) for(intj =0; J! = Len-i-1; ++j)if(Arr[j]->score > arr[j+1]->score) Swap (Arr[j], arr[j+1]);} Ostream& Print (ostream& OS,Conststudent&Stu) {OS<< Stu.name <<" "<<Stu.score; returnos;}intMain () {/*int a = 3, b = 4; Swap (A, b); printf ("%d%d\n", A, b); */Srand ((int) Time (0)); /*int arr[n]; for (int i = 0; I! = N; ++i) {printf ("%4d", arr[i] = rand ()%100 + 1); } printf ("\ n"); Sort (arr, N); for (i = 0; I! = N; ++i) {printf ("%4d", Arr[i]); } printf ("\ n"); */Student*Stu[n]; for(inti =0; I! = N; ++i) {print (cout,* (Stu[i] =NewStudent ("Jack", rand ()% -+1))) <<Endl; } printf ("******************************************\n"); Sort (stu, N); for(i =0; I! = N; ++i) {print (cout,*stu[i]) <<Endl; } return 0;}
C + + Notes