以文本方式查看主题 - 中文XML论坛 - 专业的XML技术讨论区 (http://bbs.xml.org.cn/index.asp) -- 『 C/C++编程思想 』 (http://bbs.xml.org.cn/list.asp?boardid=61) ---- 常用算法(C++描述)--网友提供 (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=53144) |
-- 作者:DMman -- 发布时间:9/27/2007 12:54:00 PM -- 常用算法(C++描述)--网友提供 网友提供,借花献佛了~ 由于最近看到论坛上的朋友的问题有很多是重复,所以开个接龙帖子,大家把经常用的算法以接龙的方式跟帖下去,以便后来者查询,请大家踊跃跟贴,请勿灌水,有问题请另外开贴提问! 要求:写清楚题目,算法思想和注释。 索引的形式整理在1楼,标有“算法思想”的代表只给出最简单算法主干。 1楼、索引 10、Selection Algorithm
|
-- 作者:DMman -- 发布时间:9/27/2007 12:54:00 PM -- 裴波那契数列 //循环算法(求前n个fibonacci数) /*递归算法(求第n个fibonacci数) |
-- 作者:DMman -- 发布时间:9/27/2007 12:54:00 PM -- josephus问题(算法思想) #include<iostream.h> void main() for(int i=0;i<N;i++) cout<<"please input the Count off number: "; while(leave!=N-1) //当离开人数不等于总人数 if(*p!=0) //已离开的小孩不用报数 if(count==interval) //报到此数时 p=kid; while(*p==0) //最后只剩下一个号码不是0的小孩 cout<<endl<<"the last one is: "<<*p<<endl; |
-- 作者:DMman -- 发布时间:9/27/2007 12:54:00 PM -- 遍历国际象棋棋盘 enum bin{fal,tr}; struct stack void push(stack it) stack pop() bin empty() void main()
|
-- 作者:DMman -- 发布时间:9/27/2007 12:55:00 PM -- 八皇后问题(算法思想) 问题:在国际象棋棋盘上放置八个皇后(很强的:-),使她们互不攻击,问共有多少种方法? 算法:可以用穷举法,穷举法通过循环或者递归来实现;还可以用试探法,同样可以用递归来实现(包含回溯).我想解决问题的核心就是算法,知道算法了,具体用什么语言来实现就不是很难了. #include<stdio.h> #define N 8 void main() |
-- 作者:DMman -- 发布时间:9/27/2007 12:55:00 PM -- 汉诺塔问题 #include<iostream.h> void hanio(int n,char,char,char); void main() void hanio(int n,char A,char B,char C) 运行结果是: 这个不是从递归算法出发用栈消解得出的算法,而是直接从当前情况来判断移动的步骤。非递归算法: #include<iostream.h> using namespace std; class Needle void Hanoi(int n) void main() |
-- 作者:DMman -- 发布时间:9/27/2007 12:55:00 PM -- 矩阵算法 1 1 1 1 1 1 #include<iostream.h> 以1 1 1 1为例,分成两部分: 1 1 1 1 1 对应的数组坐标为: (0,3) (0,0)(0,1)(0,2)(0,3) 对应的判断为: if((i+j)>=m) if((i+j)<=m) a[i][j]=(i>=j)?(m-i):(m-j); a[i][j]=(i<=j)?(i+1):(j+1); 这样就可以确定赋值给上半边还是下半边了! 接下来的问题就是打印输出了! if(j==(m-1)) |
-- 作者:DMman -- 发布时间:9/27/2007 12:56:00 PM -- 符号转换表 Digraphs: Trigraphs: |
-- 作者:DMman -- 发布时间:9/27/2007 12:56:00 PM -- 计算器的,(但是太急了,只是后缀输入,不要 介意啊,以后扑上) #include<iostream.h> 输入: 4 3 * 2 + = 回车 输出 14 |
-- 作者:DMman -- 发布时间:9/27/2007 12:57:00 PM -- Selection Algorithm, 该算法的功能是:1。Sorting the list in decreasing order, and then the Kth largest value will be in position K. 2。A related technique to this would be to find the largest value and then move it to the last location in the list. If we again look for the largest value in the list, ignoring the value we already found, we get the second largest value, which can be moved to the second last location in the list. If we con tinue this process, we will find the Kth largest value on the Kth pass. This gives the algorithm: #include"iostream" using namespace std; void Find_Kth_Largest(int list[],int n,int k){ //list:the value to look through //n:the size of the list //k:the element to select int i,largest,largest_location,j; for(i=1;i<=k;i++){//for_1 largest=list[1]; largest_location=1; for(j=2;j<=n-(i-1);j++){//for_2 if(list[j]>largest){ largest=list[j]; largest_location=j; }//if }//for_2 swap(list[n-(i-1)],list[largest_location]);//exchanging, making the largest elements //in the last elements of the list }//for_1 cout<<"The number is:"<<largest<<"\n"; }//Find_Kth_Largest void swap(int &a,int &b){//exchange int temp; temp=a; a=b; b=temp; }//swap void main(){ int List[16],K,i; cout<<"Please input the K:"; cin>>K; cout<<"\n"; List[0]=0; for(i=1;i<=15;i++){ List[i]=2*i-1; List[0]++;//List[0] is stored the length of the List }//for cout<<"\n"<<List[0]<<"\n";//output the length Find_Kth_Largest(List,List[0],K); }
|
-- 作者:DMman -- 发布时间:9/27/2007 12:57:00 PM -- 偶来个,,看上去挺吓人的,其实更简单~ /*【Background】 Personally it's considered as meaningful in exercise 【Problem Description】 求n之内的所有偶数表示为两个素数之和。 【Input Example】 10 【Output】 6=3+3 8=3+5 10=3+7 */ file://IDE:VC++6.0file://long型最大值范围内求解素数(之所以只是用long,因为范围再大也是一样永远是有限个数无法完全 file://证明此命题,只是一个示例验证罢了) file://素数就用的那经典判定法:2到此数square root不能被此数整除 bool isPrime(long n) int main() |
-- 作者:DMman -- 发布时间:9/27/2007 12:58:00 PM -- 我水,就写个简单点的好了(复数——学习重载最好了) #include<iostream.h> |
-- 作者:DMman -- 发布时间:9/27/2007 12:58:00 PM -- 我把书上的Selection Algorithm编程实现了: using namespace std; void Find_Kth_Largest(int list[],int n,int k){ //list:the value to look through //n:the size of the list //k:the element to select int i,largest,largest_location,j; for(i=1;i<=k;i++){//for_1 largest=list[1]; largest_location=1; for(j=2;j<=n-(i-1);j++){//for_2 if(list[j]>largest){ largest=list[j]; largest_location=j; }//if }//for_2 swap(list[n-(i-1)],list[largest_location]);//exchanging, making the largest elements //in the last elements of the list }//for_1 cout<<"The number is:"<<largest<<"\n"; }//Find_Kth_Largest void swap(int &a,int &b){//exchange int temp; temp=a; a=b; b=temp; }//swap void main(){ int List[16],K,i; cout<<"Please input the K:"; cin>>K; cout<<"\n"; List[0]=0; for(i=1;i<=15;i++){ List[i]=2*i-1; List[0]++;//List[0] is stored the length of the List }//for cout<<"\n"<<List[0]<<"\n";//output the length Find_Kth_Largest(List,List[0],K); } |
-- 作者:liuyuanyang -- 发布时间:1/16/2009 8:32:00 PM -- 谢谢~~很好~ |
W 3 C h i n a ( since 2003 ) 旗 下 站 点 苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》 |
125.000ms |