Problem A: 默認引數:求圓面積
Time Limit: 1 Sec Memory Limit: 128 MBDescription
撰寫一個帶默認值的函式,用于求圓面積,其原型為:
double area(double r=1.0);
當呼叫函式時指定引數r,則求半徑為r的圓的面積;否則求半徑為1的圓面積,
其中,PI取值3.14,
Input
一個實數,是圓的半徑,
Output
輸出有2行,第一行是以輸入數值為半徑的圓面積,第二行是半徑為1的圓面積,
Sample Input
19Sample Output
1133.54 3.14Append 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 MBDescription
撰寫兩個名為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.3Sample 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 MBDescription
給出三個數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 0Sample Output
case 1 : 20, 10 case 2 : 20, 10 case 3 : 100, 0 case 4 : 1, -1 case 5 : 0, 0Append 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 MBDescription
撰寫一個程式,求解以下三個函式: 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 73Sample Output
256 56 144HINT
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 MBDescription
撰寫用來交換兩個數的函式,使得“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 5Sample Output
57 12 -3 9 4 -12 5 3HINT
“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; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/19474.html
標籤:C++
