|
|
摘要:/***quick sort**/#include<stdio.h>void swap(int * a,int * b){int tmp;tmp = *a;*a = * b;*b = tmp;}void quickSort(int array[],int start,int end){int i,j;if(start < end){i = start;j = end + 1;wh...
阅读全文
摘要:/***他山之石**/class stackNode{ double m_dbValue; stackNode*m_pNext; stackNode*m_pCurentmin;};class stack{stackNode*m_pTop; void push(const double v) { stackNode*p=new stackNode(); p.m_dbValue;=v; p.m_pNe...
阅读全文
摘要:/***shell's sort**/#include<stdio.h>void shellSort(int array[],int n){int gap = n,flag = 0;int tmp,j = 0;while(gap > 1){gap /= 2;do{flag = 0; for(int i = 0;i < n - gap;i++){ j = i + gap;if...
阅读全文
摘要:/***selection sort**/#define keyType int#include<stdio.h>int selectSort(keyType array[],int n){keyType tmp;for(int i = 0;i < n - 1;i++){int min = i;for(int j = i + 1;j < n;j++){if(array[j]...
阅读全文
摘要:/***bubble Sort**/#define keyType int#include<stdio.h>//base bubble sort int bubbleSort(keyType array[],int n){keyType tmp;for(int i = 1;i <= n-1;i++){for(int j = 0;j < n-i;j++){if(array[j...
阅读全文
摘要:/***Straight Insertion Sort**/#define keyType int#include<stdio.h>int insertSort(keyType array[],int n){keyType tmp;for(int i = 1;i < n;i++){tmp = array[i];int j = i -1;while(j >= 0 &&...
阅读全文
摘要:/***折半查找:适用于关键字有序的顺序表的查找**/#include<stdio.h>int binarySearch(int a[],int n, int key){int low = 0,high = n - 1;while(low <= high){int mid = (low + high)/2;if(a[mid] == key)return mid;if(a[mid]...
阅读全文
摘要:/***perfect number, if a number equals the sum of it's factor,then it is a perfect number.**/#include<stdio.h>int factorSum(int n){int sum = 0;for(int i = 1;i < n;i++){if(0 == n % i)sum += i;...
阅读全文
摘要:/***求PI,用正多边形逼近法、数值概率算法**/#include<stdio.h>#include<math.h>double getPI(int n);int main(){int n = 1;double PI;printf("please input the accuracy:\n");scanf("%d",&n);PI = getPI(n);printf...
阅读全文
摘要:/***求幂--递归法和非递归法**/#include<stdio.h>#include<math.h>//n>=0,非递归算法乘法次数为O(n)//返回主可能很大,故用unsigned longunsigned long myPower(int m,int n){int p = 1;while(--n >= 0)p = p * m;return p;}//n ...
阅读全文
摘要:/***计算组合数C(m,n)(m为下标,n为上标)**/#include<stdio.h>//use the formula: c(m,n) = c(m-1,n) +c(m-1,n-1)int cnr(int m,int n){if(m == n||0 ==n)return 1;elsereturn cnr(m-1,n)+(m-1,n-1);}int main(){int m,n;p...
阅读全文
摘要:/**输入五个字符串,按字母顺序排列输出**/#include<stdio.h>#include<string.h>int main(){printf("please input five strings:\n");char tmp[20];char cs[5][20]; //to store the stringsfor(int i = 0;i < 5;i++){g...
阅读全文
摘要:/**判断回文数**/#include<stdio.h>int isCircle(int n);int reverse(int m);int main(){int n;printf("please input a number: \n");scanf("%d",&n);if(isCircle(n)){printf("%d is a palindrome\n",n);}else{...
阅读全文
摘要:/**计算一个字节中1和0的位数**/#include<stdio.h>int bitNumber(unsigned char c,int & count1,int & count0){unsigned char tmp = 0x1;for(int i = 0;i < 8;i++){if((c & tmp) != 0) {count1 ++;}else c...
阅读全文
摘要:/**删除字符串中指定的字符**/#include<stdio.h>void delChar(char * str,char c){char * p = str; while(*p != '\0'){while(*p == c){char * q = p;do{ // do...while结构*q = * (q+1);q++;}while(*q != '\0');}p++;}}/*vo...
阅读全文
摘要:/**统计字符串中包含单词的个数**/#define MaxSize 100#include<stdio.h>int cWords(char * str){int count = 0,flag = 5; //the initial value of flag should be larger than 2char * p = str;while(*p != '\0'){if(*p &g...
阅读全文
摘要:/**实现字符串的循环右移功能,如loopMove("abcde",3)得"cdeab"**/#include<stdio.h>#include<string.h>#include<stdlib.h>/*算法一:将最后一个字符存入tmp,将前面其他各位依次后移一位,再将tmp中的数存入首位,如此循环n次,将后面的n位移至前面*/int loopMove(char...
阅读全文
摘要:/**将一个整数转换为相应的字符串形式,要求用递归和非递归方法分别实现**/#define MaxSize 50#include<stdio.h>#include<string.h>/*len为str的长度*/void reverse(char * str,int len){char tmp;for(int j = 0;j < len-1-j;j++){tmp = s...
阅读全文
摘要:/**一个数加上100后是一个完全平方数,再加上168后又是一个完全平方数,求这个数**/#include<stdio.h>#include<math.h>int func(int low,int high){ double x , y; for(int i = low; i < high;i++) {x = sqrt(i+100);y = sqrt(i+168);i...
阅读全文
摘要:/***输入字符串a1,a2,a3,...,an*输出字符串An,a1,An-1,a2,...A1,an*递归法实现**/#include<stdio.h>#include<string.h>char upper(char c){return (c-'a'+'A');}/*递归法1*/void printString(char str[],int n,int i){if(i...
阅读全文
|