第一章C++习题

第七题

#include<iostream>
using namespace std;
int main()
{
int max (int x =10, int y =20);
max();
cout << " max = " << max() << endl;
return 0;
}
int max ( int x, int y)
{
if (x>y)  return x;
else      return y;
}


第八题

#include<iostream>
using namespace std;
int sort(int &x,int &y){
if(x<y) cout<<y<<","<<x<<endl;
else cout <<x<<","<<y<<endl;
}
int main()
{
int a,b;
cout<<"请输入两个整数"<<endl; 
    cin>>a>>b;
    sort(a,b);
return 0;
}


 

第九题

#include<iostream>
using namespace std;
int paixu( int &a, int &b, int &c)
{
   if( a < b)
   {
   if( b < c)
   {
   cout << "a < b < c" << endl;
   }
   else if ( a < c )
   {
   cout << "a < c < b" << endl;
   }
   else
   {
   cout << "c < a < b" << endl;
   }
   }
   else if ( b > c )
   {
   cout << " c < b < a " << endl;
   }
   else if ( a > c)
   {
   cout << " b < a < c " << endl;
   }
   else 
   {
   cout << " b < c < a " << endl;
   }
return 0;
}
int main()
{
int i , j ,k;
cout << "a = ";
cin >> i;
cout << "b = ";
cin >> j; 
cout << "c =" ;
cin >> k;
paixu (i, j, k);
return 0;
}


 


第十题

#include<iostream>
#include<string>
using namespace std;
int main()
{ string  str1,str2;
cin>>str1;
cin>>str2;
str1=str1+str2;
cout<<str1<<","<<str2<<endl;  
return 0;
}

十一题

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

return 0;
} 



十二题

#include<iostream>
#include<string>
using namespace std;
void sort(string a ,string b ,string c, string d, string e){
string ae[5];  string t;
ae[0]=a; ae[1]=b; ae[2]=c; ae[3]=d; ae[4]=e;
for (int i=0;i<4;i++){
for(int j=i+1;j<5;j++)
{if(ae[j]<ae[i])
{t=ae[j];ae[j]=ae[i];ae[i]=t;
}
}

}
for (int i=0;i<5;i++){
cout<<ae[i]<<endl; 
}
}
int main(){
string a,b,c,d,e; 
cin>>a>>b>>c>>d>>e;
sort (a,b,c,d,e);
return 0;
} 


十四题

#include<iostream>
using namespace std;
template <typename T>
T paixu(T*a)
{
int i, j, t;
for(i = 0 ; i < 5 ; i++)
{
for (j = 0 ; j < 5-i; j++ )
{
if( a[j] > a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
  }
 }
}
return  0;
}
int main()
{ 
int a[5] = {1,55,2,3,4};
float b[5] = {1.1, 2.2,3.3,4.4,5.5};
    double c[5] = {1.11,2.22,3.33,4.44,5.11};
int i = 0;
paixu(a);
{
for(i = 0 ; i < 5; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
paixu(b);
{
for(i = 0 ; i < 5; i++)
{
cout << b[i] << " ";
}
cout << endl;
}
paixu(c);
{
for(i = 0 ; i < 5; i++)
{
cout << c[i] << " ";
}
cout << endl;
}
return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值