将往观

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Problem A: 默认参数:求圆面积

Time Limit: 1 Sec  Memory Limit: 128 MB

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

Append Code

int main()
{
    double r;
    cin>>r;
    cout<<area(r)<<endl;
    cout<<area()<<endl;
    return 0;
}

AC代码

#include <iostream>
#include <iomanip>
using namespace std;
double area(double r=1.0)
{
    return 3.14*r*r;
}

Problem B: 重载函数:max

Time Limit: 1 Sec  Memory Limit: 128 MB

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

Append Code

int main()
{
    int a,b;
    double c,d;
    cin>>a>>b;
    cout<<max(a,b)<<endl;
    cin>>c>>d;
    cout<<max(c,d)<<endl;
    return 0;
}

AC代码

#include <iostream>
#include <iomanip>
using namespace std;
int max(int a,int b)
{
    return a>b?a:b;
}
double max(double a,double b)
{
    return a>b?a:b;
}

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

Time Limit: 1 Sec  Memory Limit: 2 MB

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 
 

Append Code

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 "<<i<<" : "<<mmax<<", "<<mmin<<endl;
    }
}

AC代码

 

#include <iostream>
#include <iomanip>
using namespace std;
void get_num(int &a,int &b,int &c)
{
    cin>>a>>b>>c;
}
void max_min(int &max,int &min,int a,int b,int c)
{
    max=a>b?a:b;
    if(c>max)
        max=c;
    else
        max=max;
     min=a<b?a:b;
    if(c<min)
        min=c;
    else
        min=min;
}

Problem D: 求(x-y+z)*2

Time Limit: 1 Sec  Memory Limit: 128 MB

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 144

HINT

 

Append Code

int main()
{
    int n, x, y, z;
    while(cin>>n)
    {
        if(n == 3)
        {
            cin>>x>>y>>z;
            cout<<f(x, y, z)<<endl;
        }
        if(n == 2)
        {
            cin>>x>>y;
            cout<<f(x, y)<<endl;
        }
        if(n == 1)
        {
            cin>>x;
            cout<<f(x)<<endl;
        }
        if(n == 0)
            break;
    }
}

AC代码

 

#include <iostream>
#include <iomanip>
using namespace std;
int f(int x,int y=1,int z=0)
{
    return 2*(x-y+z);
}

 

 

 

Problem E: 编写函数:Swap (I) (Append Code)

Time Limit: 1 Sec  Memory Limit: 16 MB

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”中用到的头文件、全局变量或宏的定义应自行补充。

 

Append Code

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

AC代码

 

#include <iostream>
#include <iomanip>
using namespace std;
void Swap (int &a,int &b)
{
    int t;
    t=a;
    a=b;
    b=t;
}
void Swap(int *a,int *b)
{
    int t;
    t=*a;
    *a=*b;
    *b=t;
}
void Swap (double &a,double &b)
{
    double t;
    t=a;
    a=b;
    b=t;
}
void Swap(double *a,double *b)
{
    double t;
    t=*a;
    *a=*b;
    *b=t;
}

 

 

 

posted on 2020-09-12 20:56  将往观  阅读(92)  评论(0编辑  收藏  举报