第一章习题汇总

7.
#include<iostream>

using namespace std;
int main ()
{
int a,b;
int f(int x,int y,int z=0);
a=f(5,9);
b=f(1,5,10);
cout<<a<<endl<<b<<endl;
return 0;
}
int f(int a,int b,int c=0)
{
if(b>a) a=b;
if(c>a) a=c;
return a;
}

8.

#include<iostream>
using namespace std;
int main ()
{
int a,b;
void f(int &a,int &b);
cin>>a>>b;
    f(a,b);
return 0;
}
void f(int &a,int &b)
{
if(a>b) 
cout<<a<<b;
else if(a<b)
cout<<b<<a;

}


9.

#include <iostream>
using namespace std;
void res(int &x,int &y,int &z)
{
int temp;
if(x>y)
{
  temp=y;y=x;x=temp;
}
if(x>z)
    {
temp=z;z=x;x=temp;
}
if(y>z)
    {
temp=y;y=z;z=temp;
}
 
}
int main() 
{ 
int a,b,c;
cin>>a>>b>>c;
res(a,b,c);
cout<<a<<"  "<<b<<"  "<<c;
return 0;
}


10.

#include <iostream>
#include<string>
using namespace std;
int main() 
{
	string string1="China";
	string string2="Hello";
	string1=string1+string2;
	cout<<string1;
	return 0;
}


#include <iostream>
#include<string>
using namespace std;
int main() 
{
	int i=0;
	string string1;
	cin>>string1;
	int a=string1.length();
	for(i=string1.length()-1;i>=0;i--)
	{
		cout<<string1[i];
	}
		return 0;
}



12.

#include<iostream>
#include<string>
using namespace std;


void bubble_sort(string str[],int n)
{
    int i,j;
    string temp;
    for(j=0;j<n-1;j++)
        for(i=0;i<n-1-j;i++)
        {
            if(str[i]>str[i+1])
            {
                temp=str[i];
                str[i]=str[i+1];
                str[i+1]=temp;
            }
        }
}
void bubble_sort(string str[],int n);
int main()
{
int i;
string str[5]={"zhang","li","fan","wnag","tan"};
bubble_sort(str,5);
    for(i=0;i<5;i++)
    {
    cout<<str[i]<<"  ";
    }
}

13.

#include<iostream> 
using namespace std; 
float bubble_sort(float *a,int n) 
{  
	float t; 
	int i,j; 
	for(i=1;i<n;i++) 
	{ 
		for(j=0;j<n-i;j++) 
		{ 
		if(a[j]>a[j+1]) 
			{ 
				t=a[j+1]; 
				a[j+1]=a[j]; 
				a[j]=t; 
			} 
		} 
	}  
		for(i=0;i<n;i++) 
	{  
		cout<<a[i]<<" "; 
	}  
		cout<<endl; 
		return 0;
}  


int bubble_sort(int *a,int n) 
{ 
	int t; 
	int i,j;
	for(i=1;i<n;i++) 
	{ 
		for(j=0;j<n-i;j++) 
		{
			if(a[j]>a[j+1])
			{
			t=a[j+1]; 
			a[j+1]=a[j]; 
			a[j]=t; 
			} 
		} 
	}  
for(i=0;i<n;i++)  
{
	cout<<a[i]<<" "; 
}  
	cout<<endl; 
	return 0; 
} 
double bubble_sort(double *a,int n) 
{ 
	double t; 
	int i,j;
	for(i=1;i<n;i++)
	{ 
		for(j=0;j<n-i;j++) 
		{ 
			if(a[j]>a[j+1]) 
			{ 
				t=a[j+1]; 
				a[j+1]=a[j]; 
				a[j]=t; 
			} 
		} 
	}  
for(i=0;i<n;i++)
{  
	cout<<a[i]<<" "; 
}  
	cout<<endl; 
	return 0; 
}  
int main() 
{ 
	int a[5]={5,4,3,2,1}; 
	float b[5]={1.2,3.4,2.2,5.4,4.5}; 
	double c[5]={1.111,3.333,0.4444,5.5555,2.2222}; 
	bubble_sort(a,5); 
	bubble_sort(b,5);
	bubble_sort(c,5); 
} 


14.

#include<iostream> 
using namespace std; 
template <typename T> 
void sort(T* cp); 
int main() 
{ 
	int a[5]={5,4,3,2,1}; 
	float b[5]={2.2, 7.7, 5.5, 6.6, 3.3 }; 
	double c[5]={1.001,4.001, 7.001,5.001, 0.001}; 
	sort(a); 
	sort(b); 
	sort(c);
	return 0; 
}
template <typename T>
void sort(T* cp) 
{ 
	int i,j; 
	T temp; 
	for(i=0;i<4;i++) 
		{ 
			for(j=0;j<4;j++)  
				{ 
					if(cp[j]>cp[j+1])
					{
						temp=cp[j]; 
						cp[j]=cp[j+1]; 
						cp[j+1]=temp;
					} 
				}  
		} 
	for(i=0;i<5;i++) 
	cout<<cp[i]<<" "; 
	cout<<endl; 
}


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值