2011计算机等级考试二级C++辅导实例编程汇总

举报
资源描述
2011年计算机等级考试二级C+辅导实例编程(1)C+冒泡排序基本应用技巧分享对于学过c 语言的朋友来说,可能对于里面的冒泡排序还是有很深印象的。那么今天我们将会为大家介绍一下具有C 语言特性的C+语言中冒泡排序的实现方法。接下来就让我们一起来看看C+冒泡排序的具体操作方法吧。C+冒泡排序代码示例:#include#include/*冒泡排序paramter:int*pDala:指针数组int Count:数组大小return value:返回数组的指针*/int*BubbleSort(int*pData,int Count)int iTemp;for(int i=l;i=i;j)if(pDataj pDataj-l)iTemp=pData|j-l;pDataj-1 J=pDatajJ;pDataj=iTemp;return pData;void main()int all0=3,2,94,23,34,65,22,33,432,34);int count=sizeof(a)/sizeof(a0);int*b=BubbleSort(a,count);定义指针for(int i=O;i counl;i+)(cout *(b+i)0,c=0,d=mo d 称为该随机序列的种子。-一 般情况下,取 gcd(m,b)=l,因此可取b 为一素数。这是一个随机数类:代码const unsigned long maxshort=65535L;const unsigned long multiplier=1194211693L;const unsigned long adder=12345L;class RandomNumberprivate:/当前种子unsigned long randSeed;public:/构造函数,默认值。表示山系统自动产生种子RandomNumber(unsigned long s=0);产生0 n-1之间的随机整数unsigned short Random(unsigned long n);/产生 0,1)之间的随机实数double fRandom(););/产生种子RandomNumber:RandomNumber(unsigned long s)(if(s=0)randSeed=time(0);用系统时间产生种子elserandSeed=s;)产生0n-1 之间的随机整数unsigned short RandomNumber:Random(unsigned long n)(randSeed=multiplier*randSeed+adder;return(unsigned short)(randSeed 16)%n);/产生 0,1)之间的随机实数double RandomNumber:fRandom()(return Random(maxshort)/double(maxshort);)利用这个随机数类,写一个程序,模拟抛硬币的实验。抛 10次硬币构成个事件,每次事件记录得到正面的个数。反复模拟这个事件50,000次,然后对这50,000L 次进行输出频率图,比较每次事件得到正面次数的比例。以下是总的代码:头文件 RandomNumber.h:代码/RandomNumber.hconst unsigned long maxshort=65535L;const unsigned long multiplier=1194211693L;const unsigned long adder=12345L;#ifndef RANDOMNUMBER H#define RANDOMNUMBER_Hclass RandomNumberprivate:/当前种子unsigned long randSeed;public:/构造函数,默认值0 表示由系统自动产生种子RandomNumber(unsigned long s=0);产生0n-1之间的随机整数unsigned short Random(unsigned long n);/产生 0,1)之间的随机实数double fRandom(););#endif类实现文件 RandomNumber.cpp:代码/RandomNumber.cpp#include RandomNumber.hH#include#include#includeusing namespace std;/产生种子RandomNumber:RandomNumber(unsigned long s)(if(s=0)randSeed=time(0);用系统时间产生种子elserandSeed=s;)产生0 n-1 之间的随机整数unsigned short RandomNumber:Random(unsigned long n)randSeed=multiplier*randSeed+adder;return(unsigned short)(randSeed 16)%n);)/产生 0,1)之间的随机实数double RandomNumber:fRandom()(return Random(maxshort)/double(maxshort);)主文件Main:代码/主文件main/*Author:Tanky woo*Blog:www.WuTianQ*Date:2010.12.7*代码来至王晓东 计算机算法设计与分析*/#include nRandomNumber.h#include#include#includeusing namespace std;int TossCoins(int numberCoins)(/随机抛硬币static RandomNumber coinToss;int i,tosses=0;for(i=0;i numberCoins;+i)tosses+=coinToss.Random(2);return tosses;int main()/模拟随机抛硬币事件const int NCOINS=10;const long NTOSSES=50000L;/headsi得到的i 次正面的次数long i,headsNCOINS+1 ;int j,position;/初始化数组headsfor(j=0;j NCOINS+1;+j)heads j=0;/重复50,000次模拟事件for(i=0;i NTOSSES;+i)headstTossCoins(NCOINS)J+;/输出频率图for(i=0;i=NCOINS;+i)p o s it io n =i n t(n o a t(h e a d s i )/N T O S S E S*7 2);c o u t s e t w(6)i ”f o r(j =0;joc o u t c o u t 叫“e n d l;r e t u r n 0;输出频率图:0*1*2*345678*9*10*请按任意键继续.2011年计算机等级考试二级C+辅导实例编程(3)两个数论的算法#includeusing namespace std;struct result(int d;int x;int y;);/d=gcd(a,b)=ax+byresult ExtendeEuclid(int a,int b)(result res;if(!b)res.d=a;res.x=l;res.y=O;return res;)result temp=ExtendeEuclid(b,a%b);res.d=temp.d;res.x=temp.y;res.y=temp.x-a/b*temp.y;return res;)inline long mod(long a,long b)(return(a%b+b)%b;计算满足ax和 b 关于n 同余的xvoid ModularLinearEquationSolver(int ajnt b,int n)if(a=0lln=0)(coutvv”参数有错return;)result re=ExtendeEuclid(a,n);if(b%re.d=0)int xO=mod(re.x*(b/re.d),n);for(int i=0;i=re.d-l;i+)coutendl;elsecout无解ENDL;)int main()(ModularLinearEquationSolver(14,30,100);return 0;)2011年计算机等级考试二级C+辅导实例编程(4)常用算法之插入排序(C+版)1 vector InsertSort(vector vec)2(3 cout4 int a;5 while(cina)6 vec.push_back(a);7 int Count=vec.size();8910 for(int i=l;iCOUNT;I+)U(12 将veci插入到正确的位置13 for(int j=i;j0;j-)14 15 vector temp(l);16 if(vecjVECJ-l)17 18 templO=vecj-l;19 vecj-l=vecj;20 vecj=temp01;21)22 else23 continue;2425)26)27 for(int i=0;iCOUNT;I+)28 coutVEClIENDL;2930 return vec;3132)2011年计算机等级考试二级C+辅导实例编程常用算法之选择排序(C+版)1 vector selectSort(vector ivec)2 3 int num;4 coutv”请输入需要排序的整数序列按下ctrl+z结束输入nENDL;5 while(cinnum)6 ivec.push_back(num);7 int n=ivec.size();89 for(int i=O;iN-l;1+)10 11 int Min=i;1213 求 iveci与 ivecn之间的最小值 ivecfMin;14 for(int j=i;jN-l;J+)15 1617 if(ivecMinivecj+1 )18 Min=j+1;19202122232425262728293031323334continue;交换顺序vector temp(l);temp0=iveci;iveci=ivecMin;ivecMin=temp0;)for(int i=0;iN;I+)(coutIVECIENDL;return ivec;35)2011年计算机等级考试二级C+辅导实例编程常用算法之冒泡排序(C+版)其实我认为冒泡是最没有必要说的个算法,如果连冒泡都不知道的话,我觉得就不能谓之学过算法。这个应该是属于特别简单的一个算法,很基本,我记得当时我们是讲算法的时候,讲的第一个就是这个冒泡排序。我也就不多说其他了,我这里写上纯粹是为了 一个完整性。效率O(n*n),稳定排序。1#include2 using namespace std;34 int BubbleSort(int*nData,int len)5(6 bool isOk=false;7 for(int i=0;i8 isOk=true;9 for(int j=len-l;j i;-j)10 if(nDataj11 int temp=nDatai;12131415161718192021222324nDataj=nDataj-l;nData|j-l J=temp;isOk=false;)int main()(int pData 10=1,5,93,4,7,8,2,6,10;for(int i=0;i10;+i)cout2011年计算机等级考试二级C+辅导实例编程输入一个字符串,将其逆序后输出01#include020304050607080910111213141516#includeusing namespace std;void SetStr(string&str)(int len=str.length();char temp;for
展开阅读全文
温馨提示:
金锄头文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
相关搜索

当前位置:首页 > 商业/管理/HR > 营销创新


电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号