C++练习

1、fibonacci数列,递归的效率及函数运行时间

// Source : C++红皮书 p81
// Author : Conard
// Date   : 2015-09-20

#include <iostream>
#include <iomanip>
#include <ctime> 
using namespace std;

int fibonacci(int n);
int main()
{
	clock_t t1,t2;
	t1=clock();
	for(int i=1;i<41;i++)
	{
		cout<<setw(12)<<fibonacci(i);
		if(i%4==0)cout<<endl;
	}
	cout<<((float)(clock()-t1))/CLOCKS_PER_SEC<<" seonds"<<endl;

	t2=clock();
	int f1,f2;
	f1=1;f2=1;
	for(int k=1;k<=20;++k)
	{
		cout<<setw(12)<<f1<<setw(12)<<f2;
		if(k%2==0)cout<<endl;
		f1=f1+f2;
		f2=f1+f2;
	}
	cout<<((float)(clock()-t2))/CLOCKS_PER_SEC<<" seonds"<<endl;
	return 0;
}

int fibonacci(int n)
{
	if ((n==1)||(n==2))return 1;
	else return fibonacci(n-1)+fibonacci(n-2);
}

2、判断素数

// Source : C++红皮书 p82
// Author : Conard
// Date   : 2015-09-20

#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;

bool isPrime(int n);
int main()
{
	clock_t t;
	t=clock();
	for(int n=100;n<=50000;++n)
		if(isPrime(n))cout<<n<<endl;
	cout<<((float)(clock()-t))/CLOCKS_PER_SEC<<" seconds";
	return 0;
}

bool isPrime(int n) //inline
{
	for(int i=2;i<=int(sqrt(float(n)));++i) //sqrt(n),error C2668: “sqrt”: 对重载函数的调用不明确
		if((n%i)==0)return false;
	return true;
}

3、

// Source : C++红皮书 chapter 3 习题25
// Author : Conard
// Date   : 2015-09-20
/***************************
甲队ABC三人,乙队XYZ三人,A不和X比,C不和X、Z比,对战名单
Input: xxx
Output: xxx
***************************/
#include <iostream>
using namespace std;

int main()
{
	int a=1,b=2,c=3;
	int x,y,z,x1,y1,z1;
	for(x=1;x<=3;++x)
	{
		if(x==a||x==c)continue;
		else
		{
			x1=x;
			for(z=1;z<=3;++z)
			{
				if(z==c||z==x1)continue;
				else
				{
					z1=z;
					for(y=1;y<=3;++y)
					{
						if(y==x1||y==z1)continue;
						else  y1=y;
					}
				}
			}
		}
	}
    cout<<"a:"<<a<<"\t"<<"b:"<<b<<"\t"<<"c:"<<c<<endl;
	cout<<"x:"<<x1<<"\t"<<"y:"<<y1<<"\t"<<"z:"<<z1<<endl;
	return 0;
}

4、用多维数组名作函数参数

// Source : C++红皮书 chapter 5 例题5.8
// Author : Conard
// Date   : 2015-09-22
/***************************
输入一个3×4的矩阵,求矩阵中所有元素的最大值。
Input: XXX
Output: XXX
***************************/

#include <iostream>
using namespace std;

int matMax(int m[][4]); 

int main () 
{
  int m[3][4]={{11,32,45,67},{22,44,66,88},{15,72,43,37}};
  cout<<"the max value is "<<matMax(m)<<endl;
  return 0;
}

int matMax(int m[][4]) //必须指定第二维的大小,第一维可以不指定,也可以任意指定,如m[12][4]
{
    int i,j,max;
    max=m[0][0];
    for(i=0;i<=2;++i)
    {
        for(j=0;j<=3;++j)
            if(m[i][j]>max)max=m[i][j];
    }
    return max;
}

5、最大连续子序列和

#include <iostream>
using namespace std;

int maxSub(int *,int,int &,int &);  //int maxSub(int a[],int size,int &start,int &end)
int main()
{
	int array[]={1,11,-4,-13,-5,-200};
	int astart=0,aend=0;
	int aSize=6;
	//cout<<array<<endl<<*array<<endl;
	cout<<maxSub(array,aSize,astart,aend)<<'\t'<<astart<<'\t'<<aend<<endl; //output:12	0	0
	cout<<astart<<'\t'<<aend;        //output:0	1
	return 0;
}

int maxSub(int *a,int size,int &start,int &end)  //int maxSub(int a[],int size,int &start,int &end)
{
	start=0;
	int tempSum=0,maxSum=0;
	for(int i=0;i<size;++i)
	{
		tempSum+=*(a+i);						 //tempSum+=a[i];			
		if(tempSum>maxSum){maxSum=tempSum;end=i;}
		else if(tempSum<0){tempSum=0;start=i+1;if(start==size)start=0;}
	}
	return maxSum;
}

6、pass

posted on 2015-09-20 16:50  conard  阅读(103)  评论(0)    收藏  举报