博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
山东科技大学2020年4月9日题解
阅读量:2200 次
发布时间:2019-05-03

本文共 7191 字,大约阅读时间需要 23 分钟。

山东科技大学2020年4月9日题解

题目一重载函数:max

Description

编写两个名为max的函数,它们是重载函数 ,用于求两个整数或实数的最大值。它们的原型分别是:

int max(int a,int b);

double max(double a,double b);

返回值是a和b的最大值。

Input

输入4个数,前两个数是int类型的整数,后2个数是double类型的实数。

Output

输出2个数,每个数占一行。第一个数对应于输入的两个整数的最大值,第二个数对应于输入的两个实数的最大值。

Sample Input

1 2

1.4 1.3

Sample Output

2

1.4

题目给定的主函数

int main(){
int a,b; double c,d; cin>>a>>b; cout<
<
>c>>d; cout<
<

分析

这里提供两种完成方法,一种是函数重载,一种是函数模板的方法,同样可以解决这道题目。

标程

函数重载:

#include#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R register #define LL long long #define pi 3.14159265358979323using namespace std;inline int max(int a, int b){ return a > b ? a : b;}inline double max(double a, double b){ return a > b ? a : b;}int main(){ int a,b; double c,d; cin>>a>>b; cout<
<
>c>>d; cout<
<

函数模板:

#include#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R register #define LL long long #define pi 3.141using namespace std;template
T max(T &number_1, T &number_2){ return number_1 > number_2 ? number_1 : number_2;}int main(){ int a,b; double c,d; cin>>a>>b; cout<
<
>c>>d; cout<
<

题目二默认参数:求圆面积

Description

编写一个带默认值的函数,用于求圆面积。其原型为:

double area(double r=1.0);

当调用函数时指定参数r,则求半径为r的圆的面积;否则求半径为1的圆面积。

其中,PI取值3.14。

Input

一个实数,是圆的半径。

Output

输出有2行。第一行是以输入数值为半径的圆面积,第二行是半径为1的圆面积。

Sample Input

19

Sample Output

1133.54

3.14

题目给定的主函数

int main(){
double r; cin>>r; cout<
<

分析

注意输入的数值是double型的,函数的默认参数给定为1.0即可

标程

#include#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R register #define LL long long #define pi 3.14using namespace std;inline double area(double r = 1.0){ return pi * r * r;}int main(){ double r; cin>>r; cout<
<

题目三编写函数:三个数的最大最小值 (Append Code)

Description

给出三个数a,b,c,最大值是?最小值是?

编写以下两个函数:

get_num()的功能是读取输入的三个整数a,b,c;

max_min()的功能是求出a,b,c的最大值和最小值。

以上函数的调用格式见“Append Code”。这里不给出函数原型,请通过main()函数自行确定。

Input

输入的第一个整数n,表示有n组测试数据,每组3个整数:a,b,c。a,b,c都在int类型范围内。

Output

每组测试数据对应输出一行:为a,b,c的最大值和最小值,格式见sample。

Sample Input

5

20 15 10
10 15 20
100 100 0
0 1 -1
0 0 0

*Sample Output

case 1 : 20, 10

case 2 : 20, 10
case 3 : 100, 0
case 4 : 1, -1
case 5 : 0, 0

题目给定的主函数

int main(){
int cases; int mmax, mmin, a, b, c; cin>>cases; for(int i = 1; i <= cases; ++i) {
get_num(a, b, c); max_min(mmax, mmin, a, b, c); cout<<"case "<<<" : "<
<<", "<
<

分析

由于题目给定的主函数的形式不能使用指针来传递参数,在函数取最大值的过程中使用三目运算符可以减少代码量。

标程

#include#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R register #define LL long long #define pi 3.14using namespace std;inline void get_num(int &a, int &b, int & c){ scanf("%d%d%d", &a, &b, &c);}inline void max_min(int &mmax, int &mmin, int a, int b, int c){ int number; number = a > b ? a : b; mmax = number > c ? number : c; number = a < b ? a : b; mmin = number < c ? number : c;}int main(){ int cases; int mmax, mmin, a, b, c; cin>>cases; for(int i = 1; i <= cases; ++i) { get_num(a, b, c); max_min(mmax, mmin, a, b, c); cout<<"case "<
<<" : "<
<<", "<
<

第四题求(x-y+z)*2

Description

编写一个程序,求解以下三个函数:

f(x,y,z)=2*(x-y+z)

f(x,y) =2*(x-y)

f(x) =2*(x-1)

函数调用格式见append.cc。

append.cc中已给出main()函数。

Input

输入的测试数据为多组。每组测试数据的第一个数是n(1<=n<=3),表示后面有n个整数。

当n为3时,后跟3个输入为x,y,z;

当n为2时,后跟2个输入为x,y;

当n为1时,后跟1个输入为x;

当n为0时,表示输入结束

输入的n不会有其他取值。

所有运算都不会超出int类型范围。

Output

每组测试数据对应一个输出。输出x-y+z的值。

Sample Input

3 121 38 45

2 39 11
1 73

Sample Output

256

56
14

题目给定主函数

int main(){
int n, x, y, z; while(cin>>n) {
if(n == 3) {
cin>>x>>y>>z; cout<
>x>>y; cout<
>x; cout<
<

分析

同样是函数重载的题目。

标程

#include#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R register #define LL long long #define pi 3.14using namespace std;inline int f(int x, int y, int z){ return (x - y + z) * 2;}inline int f(int x, int y){ return (x - y) * 2;}inline int f(int x){ return 2 * (x - 1);}int main(){ int n, x, y, z; while(cin>>n) { if(n == 3) { cin>>x>>y>>z; cout<
>x>>y; cout<
>x; cout<
<

题目五编写函数:Swap (I) (Append Code)

Description

编写用来交换两个数的函数,使得“Append Code”中的main()函数能正确运行

用C实现三个函数int_swap()、dbl_swap()、SWAP(),其中SWAP()是个带参宏。

用C++实现两个函数,都以Swap()命名。

以上函数的调用格式见“Append Code”。这里不给出函数原型,它们的参数请通过main()函数自行确定。

Input

输入为4行,每行2个数。

Output

输出为4行,每行2个数。每行输出的两数为每行输入的逆序。

Sample Input

12 57

9 -3
-12 4
3 5

Sample Output

57 12

-3 9
4 -12
5 3

HINT

“Append Code”中用到的头文件、全局变量或宏的定义应自行补充。

题目给定主函数

int main(){
int x1, y1; cin>>x1>>y1; Swap(&x1, &y1); cout<
<<" "<
<
>x1>>y1; Swap(x1, y1); cout<
<<" "<
<
>x2>>y2; Swap(&x2, &y2); cout<
<<" "<
<
>x2>>y2; Swap(x2, y2); cout<
<<" "<
<

分析

同样是函数重载的题目,但是这里需要写四个函数,因为这里有两种数据类型和两种传参方式,所以这里需要写四种重载的函数。

标程

#include#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R register #define LL long long #define pi 3.14using namespace std;inline void Swap(double *x, double *y){ double temp = *x; *x = *y, *y = temp;}inline void Swap(int *x, int *y){ int temp = *x; *x = *y, *y = temp;}inline void Swap(int &x, int &y){ int temp = x; x = y, y = temp;}inline void Swap(double &x, double &y){ double temp = x; x = y, y = temp;}int main(){ int x1, y1; cin>>x1>>y1; Swap(&x1, &y1); cout<
<<" "<
<
>x1>>y1; Swap(x1, y1); cout<
<<" "<
<
>x2>>y2; Swap(&x2, &y2); cout<
<<" "<
<
>x2>>y2; Swap(x2, y2); cout<
<<" "<
<

题目六Say Hello Today

Description

输出一句话,告诉今天的日期。

补全代码使append.cc的程序能够运行。

Input

输入三个整数,分别为年月日。

Output

输出格式见样例。

Sample Input

2013 4 3

Sample Output

Hello, today is 2013-4-3.

Hello, today is 2013-4-1.
Hello, today is 2013-1-1.
Hello, today is 2000-1-1.

HINT

试试函数的默认形参。

题目给定的主函数

int main(){
int yy, mm, dd; cin>>yy>>mm>>dd; SayHello(yy, mm, dd); SayHello(yy, mm); SayHello(yy); SayHello();} void SayHello(int year, int month, int day){
cout<<"Hello, "; cout<<"today is "; cout<

分析

这里用了函数主体在主函数之后的一种写法,比较少见,就在函数开头将函数开头和函数的默认参数补充完整即可。

标程

#include#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R register #define LL long long #define pi 3.14using namespace std;void SayHello(int year = 2000, int month = 1, int day = 1);int main(){ int yy, mm, dd; cin>>yy>>mm>>dd; SayHello(yy, mm, dd); SayHello(yy, mm); SayHello(yy); SayHello();}void SayHello(int year, int month, int day){ cout<<"Hello, "; cout<<"today is "; cout<

转载地址:http://psrub.baihongyu.com/

你可能感兴趣的文章
Leetcode C++ 随手刷 547.朋友圈
查看>>
手抄笔记:深入理解linux内核-1
查看>>
内存堆与栈
查看>>
Leetcode C++《每日一题》20200621 124.二叉树的最大路径和
查看>>
Leetcode C++《每日一题》20200622 面试题 16.18. 模式匹配
查看>>
Leetcode C++《每日一题》20200625 139. 单词拆分
查看>>
Leetcode C++《每日一题》20200626 338. 比特位计数
查看>>
Leetcode C++ 《拓扑排序-1》20200626 207.课程表
查看>>
Go语言学习Part1:包、变量和函数
查看>>
Go语言学习Part2:流程控制语句:for、if、else、switch 和 defer
查看>>
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
leetcode 130. Surrounded Regions
查看>>
【Python】详解Python多线程Selenium跨浏览器测试
查看>>
Jmeter之参数化
查看>>
Shell 和Python的区别。
查看>>
Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
查看>>
Loadrunner之https协议录制回放报错如何解决?(九)
查看>>
python中xrange和range的异同
查看>>