PTA——c++函数

1.在C++中,关于下列设置缺省参数值的描述中,()是正确的。

在指定了缺省值的参数右边,不能出现没有指定缺省值的参数;

 2.使用地址作为实参传给形参,下列说法正确的是()

实参与形参操作的是同一对象

3.下面程序运行正确的是:

#include<iostream>
using namespace std;

void fun(int * a, int * b) {
    int x = *a;
    *a = *b;
    *b = x;
    cout << *a << *b << " ";
}
int main() {
    int x = 1, y = 2;
    fun(&x, &y);
    cout << x << y << endl;
    return 0;
}

21 21

4.当一个函数功能不太复杂,但要求被频繁调用时,选用____。

内联函数

5.重载函数在调用时选择的依据中,错误的是(  )

 函数的参数

 ---------------------------------------------------------------------

设计一个void类型的函数Swap,该函数有两个引用类型的参数,函数功能为实现两个整数交换的操作。

 

#include <iostream>
using namespace std;

/* 请在这里填写答案 */

int main()
{
    int a, b;

    cin >> a >> b;

    Swap(a, b);

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

    return 0;
}

输入样例:3   5

输出样例:5   3

void Swap(int &x,int &y){
    int m;
    m=x;
    x=y;
    y=m;
}

设计一个void类型的函数reverse_string,其功能是将一个给定的字符串逆序。例如,给定字符串为“hello”,逆序后为“olleh”。 ###函数接口定义如下:

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

/* 你的代码将嵌在这里 */

int main(int argc, char const *argv[])
{
    string str;
    getline(cin, str);        //输入字符串
    reverse_string(str);     //逆序字符串str
    cout << str << endl;    //输出逆序后的字符串
    return 0;
}

输入样例:hello

输出样例:olleh

void reverse_string(string &s){
    int N=s.length();
    for(int i=0;i<N/2;i++){
        char temp;
        temp = s[i];
        s[i] = s[N-i-1]; 
        s[N-i-1] = temp;
    }
}

 


 

 
 
posted @ 2021-04-03 16:56  天岁  阅读(521)  评论(0编辑  收藏  举报