C++第一章的题目

来源:互联网 发布:淘宝一阳指在哪里 编辑:程序博客网 时间:2024/05/22 18:45

第一章

T7

#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;

 

}

 

 

 

 

T8

#include<iostream>

 

using namespace std;

 

int max( int &a, int &b)

{

if(a > b)

cout << a << " "<< b << endl;

else 

cout << b << " "<< a << endl;

 

return 0;

}

 

int main()

{

 

 

int i , j;

 

cin >> i >> j;

 

max( i , j );

 

return 0;

}

 

 

T9

#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;

}

 

 

 

T10

#include<iostream>

 

#include<string>

 

using namespace std;

 

int main()

{

string a = "abc";

 

string b = "cba";

 

a = a + b;

 

cout << " a = " << a << endl;

 

return 0;

}

 

 

 

T11

#include<iostream>

 

#include<string>

 

using namespace std;

 

int main()

{

string word;

 

cout << "请输入字符串: " ;

 

cin >> word;

 

int i;

 

cout <<"倒序的字符串是: "; 

 

    for ( i = word.length(); i > 0 || i== 0; i--)

 

cout <<  word[i];

 

    cout << endl;

    

  

return 0;

}

 

 

 

T12

#include<iostream>

 

#include<string>

 

using namespace std;

 

void jixupaixu(string a[])

{

int i, j ;

string temp;

for(i = 0; i < 4; i++)  //  为什么这里是排3下。5个字符串啊。  排四下就错。。

{

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

{

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

{

temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

}

}

}

}

 

int main()

{

string a[5];

int i;

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

{

cin >> a[i];

}

jixupaixu(a);

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

{

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

}

return 0 ;

}

 

 

 

T13

#include<iostream>

 

using namespace std;

 

int paixu( int *a )

{

int i , j , t;

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

{

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

{

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

{

t = a[j];

a[j] = a[j+1];

a[j+1] = t;

}

}

}

return 0;

}

float paixu( float *a )

{

int i , j , t;

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

{

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

{

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

{

t = a[j];

a[j] = a[j+1];

a[j+1] = t;

}

}

}

return 0;

}

 

double paixu( double *a )

{

int i , j , t;

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

{

for( j = 0; j < 10 - 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[10] = {1, 5 ,9 ,99, 777, 234, 213, 321,5452,42};

float b[10] = { 1.1, 22.1, 9.9, 55.1, 66.02, 66.1, 22.2, 44.9, 489.14564, 1.2365};

double c[10]  = {1,2,3,4,5,6,7,9,8,40};

int i;

    paixu(a);

{

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

{

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

}

cout << endl;

}

    paixu(b);

 

{

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

{

cout << b[i] <<" ";

}

cout << endl;

}

    paixu(c);

{

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

{

cout << c[i] <<" ";

}

cout << endl;

}

return 0;

}

 

 

 

T14

#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