C++算法编程
C++算法编程
奇遇世界
Late in autumn
展开
-
冒泡排序法
#include using namespace std;void swap(int &a,int &b){ /*int tmp=a; a=b; b=tmp; */ if(a!=b) { a^=b; b^=a; a^=b; }} void bubble_sort(int *unsorted,int len){ int i原创 2013-12-06 18:42:19 · 441 阅读 · 0 评论 -
稀疏数组
二维数组:求某一元素在数组中的位置。1、以行为主: data[i][j]的内存位置=第一个元素的位置+[ (i * 每一行元素个数)+j ] * ( 数据类型所占空间大小) 2、以列为主: data[i][j]的内存位置=第一个元素的位置+[ (j * 每一行元素个数)+i ] * ( 数据类型所占空间大小)原创 2013-12-09 10:43:25 · 533 阅读 · 0 评论 -
老鼠走迷官(一)
老鼠的走法有上、左、下、右四个方向,在每前进一格之后就选一个方向前进,无法前 进时退回选择下一个可前进方向,如此在阵列中依序测试四个方向,直到走到出口为止。 #include using namespace std;int visit(int,int);int maze[7][7] = { {2, 2, 2, 2, 2, 2, 2}, {2, 0, 0, 0,原创 2013-12-08 12:54:57 · 653 阅读 · 0 评论 -
老鼠走迷宫 (栈实现)
#include <iostream>using namespace std;//定义四个方向#define EAST MAZE[x][y+1]#define WEST MAZE[x][y-1]#define SOUTH MAZE[x+1][y]#define NORTH MAZE[x-1][y]//起点(1,1),终点(8,10)const in...原创 2013-12-08 19:06:13 · 2449 阅读 · 1 评论 -
下三角形数组转换为一维数组
(1) 将大小为n*n的下三角数组转换成以行为主的一维数组,且不存储内容为0的元素!data[i][j]的位置 =i*(i+1)/2+j; (2) 将大小为n*n的下三角数组转换成以列为主的一维数组,且不存储内容为0的元素!data[i][j]的位置=[n+(n-j+1)]*j/2+(i-j) (1)原创 2013-12-09 12:04:43 · 2442 阅读 · 0 评论 -
上三角形数组转换为一维数组
(1)将大小为n*n的上三角数组转换成以行为主的一维数组,且不存储内容为0的元素!data[i][j]的位置=[n+(n-i+1)]*i/2+(j-i) (2)将大小为n*n的上三角数组转换成以列为主的一维数组,且不存储内容为0的元素!data[i][j]的位置=j*(j+1)/2+i (1) #include #include using namesp原创 2013-12-09 11:44:28 · 3649 阅读 · 1 评论 -
螺旋队列
#include #include using namespace std;#define max(a,b) ((a)>(b)?(a):(b))#define abs(a) ((a)>0?(a):(-a)) int foo(int x,int y){ int t=max(abs(x),abs(y)); int u=t+t; int v=u-1; v=原创 2013-12-10 15:46:38 · 450 阅读 · 0 评论 -
N*N矩阵
#include #include using namespace std; int main(){ int N,s,i,j,squa; cout cin>>N; int **a=new int*[N]; if(NULL==a) return 0; for(i=0;i a[i]=new int[N]; squa=N*N;原创 2013-12-10 15:50:20 · 1384 阅读 · 0 评论 -
数字斜塔
#include #include using namespace std; int main(){ int arr[40][40]; int i,j,num; arr[0][0]=1; cout cin>>num; for(i=0;i { if(i>0) arr[i][0]=arr[i-1][0]+i; for(j=0原创 2013-12-10 15:41:19 · 774 阅读 · 0 评论 -
折半查找的递归与非递归方法实现
#include using namespace std; //折半查找的递归方法实现int BSearch(int *arr,int key,int low,int high){ if(low>high) return -1; int mid=(low+high)/2; if(key==arr[mid]) return mid; else if(k原创 2013-12-13 10:20:56 · 1394 阅读 · 0 评论 -
选择排序法
#include using namespace std;void swap(int &a,int &b){ /*int tmp=a; a=b; b=tmp; */ if(a!=b) { a^=b; b^=a; a^=b; }} void select_sort(int *unsorted,int len){ int i原创 2013-12-06 18:49:04 · 443 阅读 · 0 评论 -
堆排序法
#include using namespace std;void swap(int &a,int &b){ if(a!=b) { a^=b; b^=a; a^=b; }} void HeapAdjust(int *unsorted,int parent,int len){ int tmp=unsorted[parent]; int原创 2013-12-06 20:14:00 · 486 阅读 · 0 评论 -
归并排序法
#include using namespace std;//合并数组void MergeArray(int *unsorted,int *sorted,int first,int mid,int last){ int i=first,j=mid+1,k=0; while(i { if(unsorted[i] sorted[k++]=unsorted[i+原创 2013-12-06 20:28:54 · 466 阅读 · 0 评论 -
实现把一个字符串的后steps位移动到字符串前面
#include using namespace std; void LoopMove ( char *pStr, int steps ) { int n = strlen( pStr ) - steps; char tmp[10]; //假设steps为2 //把pStr+n即从'h'位置起steps长度的内容即”hi"复制到tmp中 memcpy( tm原创 2013-12-09 09:41:28 · 739 阅读 · 0 评论 -
老鼠走迷官(二)
由于迷宫的设计,老鼠走迷宫的入口至出口路径可能不只一条,如何求出所有的路径。只要在老鼠走至出口时显示经过的路径,然后退 回上一格重新选择下一个位置继续递回就可以了! #include using namespace std;int maze[9][9] = { {2, 2, 2, 2, 2, 2, 2, 2, 2}, {2, 0, 0, 0, 0, 0, 0, 0, 2原创 2013-12-08 12:59:43 · 673 阅读 · 0 评论 -
字符串语句逆向输出
#include int main(){ char str[30]; gets(str); for(int i=strlen(str)-1,j=0;i>j;i--,j++) { char tmp=str[i]; str[i]=str[j]; str[j]=tmp; } puts(str); return 0;}原创 2013-12-06 23:06:26 · 601 阅读 · 0 评论 -
快速排序法
#include using namespace std;void quick_sort(int *unsorted,int low,int high){ if(low { int i=low,j=high,tmp=unsorted[low]; while(i { while(i=tmp) j--; if(i unsorte原创 2013-12-06 18:55:32 · 429 阅读 · 0 评论 -
直接插入排序法
#include using namespace std;void insert_sort(int *unsorted,int len){ int i,j; for(i=1;i { int tmp=unsorted[i]; for(j=i-1;j>=0 && unsorted[j]>tmp;j--) { unsorted[j+1]=unsorted原创 2013-12-06 18:58:59 · 674 阅读 · 0 评论 -
鸡尾酒排序法
#include using namespace std;void swap(int &a,int &b){ if(a!=b) //如果a==b,则调用swap函数后会使这个数变为0 { a^=b; b^=a; a^=b; }}void Cocktail_sort(int *unsorted,int len){ bool swaped=原创 2013-12-06 19:07:16 · 904 阅读 · 0 评论 -
求最大公约数与最小公倍数
#include using namespace std;/*递归法求最大公约数int gcd(int m,int n){ if(n==0) return m; else return gcd(n,m%n);}*/int gcd(int x,int y){ while(x!=y) { if(x>y) x-=y; e原创 2013-12-06 21:48:56 · 531 阅读 · 0 评论 -
将一个数转换成n进制(2<=n<=26)
#include #include #include using namespace std;char invert(int n){ for(int i=0;i { if(n==i) return (i+'0'); }} int main(){ int num,n; string str=""; cout cin>>nu原创 2013-12-07 10:35:21 · 755 阅读 · 0 评论 -
希尔排序法
#include using namespace std;void shell_sort(int *unsorted,int len){ int i,j,gap; for(gap=len/2;gap>0;gap/=2) { for(i=gap;i { int tmp=unsorted[i]; for(j=i-gap;j>=0 && unsorte原创 2013-12-06 19:00:54 · 490 阅读 · 0 评论 -
巴斯卡三角形, 即杨辉三角
#include #include using namespace std;int main(){ int N,i,j; cout cin>>N; int **a=new int*[N]; for(i=0;i a[i]=new int[N]; cout for(i=0;i { a[i][0]=a[i][i]=1; //输入每一行数前原创 2013-12-07 14:49:26 · 824 阅读 · 0 评论 -
三色旗问题
//红白蓝三种颜色分别用0,1,2表示。//开始时0,1,2是乱序。//排列后0在前,1在中,2在后。 #include using namespace std;const int N=10;void swap(int &a,int &b){ if(a!=b) { a^=b; b^=a; a^=b; }} void shuf原创 2013-12-07 10:53:59 · 636 阅读 · 0 评论 -
求1-2+3- …… +n 的值!
#include using namespace std;long fn(long n){ while(n { cout cout cin>>n; } if(n%2==0) return (-1)*(n/2); else return (-1)*(n/2)+n;} int main(){ int n; cout原创 2013-12-06 22:01:27 · 960 阅读 · 0 评论 -
将一个正整数逆向输出
#include int main(){ int n,d; std::cin>>n; do{ d=n%10; std::cout n/=10; }while(n!=0); std::cout}原创 2013-12-06 22:06:26 · 1588 阅读 · 0 评论 -
用递归法将一个字符串逆向输出
#include using namespace std;char str[30];int len=0;void reverse(int n){ if(n { reverse(n+1); cout }} int main(){ cin>>str; len=strlen(str); cout cout reverse(0原创 2013-12-06 20:52:07 · 1054 阅读 · 0 评论 -
删除数组中重复出现的元素,只留下一个,排序后输出
#include #include using namespace std;//快速排序void quick_sort(int *unsorted,int low,int high){ if(low { int i=low,j=high,tmp=unsorted[low]; while(i { while(i=tmp) j--;原创 2013-12-07 22:51:26 · 1719 阅读 · 0 评论 -
删去字符串中包含的a、e、i、o、u字符
#include using namespace std;char *removeStr(char *p){ char *head=new char,*q=NULL; q=head; while(NULL!=p && '\0'!=*p) { if('a'==*p || 'e'==*p || 'i'==*p || 'o'==*p || 'u'==*p) p+原创 2013-12-06 20:45:39 · 995 阅读 · 0 评论