第一章C++习题

来源:互联网 发布:node express作用 编辑:程序博客网 时间:2024/05/29 10:38

第七题

#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
原创粉丝点击