C++ 第一章编程题

1.7  #include<iostream>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b;
int max(int a,int b,int c=5);
cout<<"max(a,b,c)="<<max(a,b)<<endl;
return 0;
}
int max(int a,int b,int c=5)
{
if(a<b)
a=b;
if(a<c)
a=c;
return a;

}

1.8   #include<iostream>
using namespace std;
int max(int &x,int &y)
{
if(x<y)
x=y;
return x;
}
int main()
{
int a,b;
cin>>a>>b;
cout<<"max(a,b)="<<max(a,b)<<endl;
return 0;
}

1.9  

 #include<iostream>
using namespace std;
float sort(float &x,float &y,float &z)
{
float t;
if(x>y)
{t=x; x=y; y=t;}
if(x>z)
{t=x;x=z;z=t;}
if(y>z)
{t=y;y=z;z=t;}
cout<<x<<y<<z<<endl;
return 0;
}
int main()
{
float a,b,c;
cin>>a>>b>>c;
sort(a,b,c);
return 0;
}
1.10 #include<iostream>
#include<string>
using namespace std;
int main()
{
string str1,str2;
cin>>str1>>str2;
str1=str1+str2;
cout<<str1;
return 0;
}
1.11  #include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
int i,j;
int length=sizeof(str);
    for(i=length-1;i>=0;i--)
    {
    cout<<str[i];
    }

return 0;
}
1.12 #include<iostream>
#include<string>
using namespace std;
void bubble_sort(string a[],int n)
{
int i,j;
string temb;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
 if(a[j]>=a[j-1])
 {
temb=a[j];
a[j]=a[j-1];
a[j-1]=temb;
 }
   }
    }
}
int main()
{
int n;
string str[5];
for(n=0;n<5;n++)
{
cin>>str[n];
}


bubble_sort(str,5);
for(n=0;n>5;n++)
{
cout<<str[n];
}
return 0;
}
1.13  #include <iostream>
#include <string>
using namespace std;
int main( )
{
  longa[5]={10100,-123567, 1198783,-165654, 3456};
  intb[5]={1,9,0,23,-45};
  floatc[5]={2.4, 7.6, 5.5, 6.6, -2.3 };
  voidsort(long [ ]);
void sort(int [ ]);
  voidsort(float [ ]);
  sort(a);
  sort(b);
  sort(c);
  return 0;
}
void sort(long a[ ])
{
int i, j;
 long t;
 for (j=0;j<5; j++)
   for(i=0;i<5-j;i++)
       if(a[i]>a[i+1])
                     {t=a[i];a[i]=a[i+1];a[i+1]=t;}
 cout<<"the sorted numbers : "<<endl;
 for(i=0;i<5;i++)
           cout<<a[i]<< " ";
 cout<<endl<<endl;
}
1.14  #include <iostream>


#include <string>


using namespace std;


int main( )


{ long a[5]= {10100,-123567, 1198783,-165654, 3456};


int b[5]={1,9,0,23,-45};


  floatc[5]={2.4, 7.6, 5.5, 6.6, -2.3 };


  voidsort(int [ ]);


  voidsort(float [ ]);


  voidsort(long [ ]);


sort(a);                            //对长整型数据排序


 sort(b);                            //对整型数据排序


 sort(c);                            //对单精度型数据排序


  return 0;


}


 


void sort(long a[ ])                     //对长整型数据用选择法排序的函数


{int i,j,min;


 long t;


 for(i=0;i<5;i++)


   {min=i;


    for(j=i+1;j<5;j++)


           if(a[min]>a[j]) min=j;


             {t=a[i]; a[i]=a[min];       a[min]=t; }


 cout<<"the sorted numbers : "<<endl;


 for(i=0;i<5;i++)


cout<<a[i]<<" ";


 cout<<endl<<endl;


}


 


 


void sort(int a[ ])                        //对整型数据用选择法排序的函数


{int i, j, t;


 for (j=0;j<5; j++)


   for(i=0;i<5-j;i++)


       if(a[i]>a[i+1])


                   {t=a[i];a[i]=a[i+1];a[i+1]=t;}


 cout<<"the sorted numbers : "<<endl;


 for(i=0;i<5;i++)


cout<<a[i]<< " ";


 cout<<endl<<endl;


 }


 


void sort(float a[ ])                     //对单精度型数据用起泡法排序的函数


{int i, j;


 float t;


 for(j=0;j<5;j++)


   for(i=0;i<5-j;i++)


       if(a[i]>a[i+1])


              {t=a[i];a[i]=a[i+1];a[i+1]=t;}


 cout<<"the sorted numbers : "<<endl;


 for(i=0;i<5;i++)


cout<<a[i]<< " ";


 cout<<endl<<endl;


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值