【嵙大o】C++作业合集
参考:
C++ swap(交换)函数 指针/引用/C++自带-CSDN博客
Problem ID | Title | |
CPP指针CPP引用 | 1107 Problem A | 编写函数:Swap (I) (Append Code) |
1158 Problem B | 整型数据的输出格式 | |
1163 Problem C | 时间:24小时制转12小时制 | |
1205 Problem D | 应该付多少钱? | |
1280 Problem E | 默认参数:求圆面积 | |
1281 Problem F | 类的初体验 | |
1639 Problem G | 重载函数min() | |
2265 Problem H | 函数重载:求面积 | |
2277 Problem I | C++的初体验 | |
2278 Problem J | string的初体验 |
Problem A: 编写函数:Swap (I) (Append Code)
Time Limit: 1 Sec Memory Limit: 16 MB
Submit: 21275 Solved: 13326
[Submit][Status]
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;
}
#include <iostream>
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;
}
Problem B: 整型数据的输出格式
Time Limit: 1 Sec Memory Limit: 2 MB
Submit: 25730 Solved: 11500
[Submit][Status]
Description
输入2个整数,按照指定的格式输出这两个数。
Input
两个整数0<=a,b<=1000。a与b之间用一个空格隔开。
Output
输出有3行,第一行是:“Octal Decimal Hexadecimal”,每个单词之间用空格隔开。第二行和第三行分别是输入的两个整数a、b的八进制数、十进制数和十六进制数。输出时,每个数字左对齐,且八进制、十进制和十六进制数据分别与第一行中的字母O、D、H对齐。
Sample Input
13 456
Sample Output
Octal Decimal Hexadecimal 15 13 d 710 456 1c8
HINT
注意printf的格式控制符的使用,如何控制每个数据输出的位宽以及对齐方式?
Append Code
Problem C: 时间:24小时制转12小时制
Time Limit: 1 Sec Memory Limit: 2 MB
Submit: 16619 Solved: 5663
[Submit][Status]
Description
编写一个程序,把输入的24小时制的时间,转换12小时制的格式输出。
12小时制没有0点时段,是以数字12、1、2、3、4、5、6、7、8、9、10、11依次序表示每个时段的。
正午是“12:00 p.m.”,也就是24小时制的中午12点;
24小时制的12:00~12:59,是12小时制的12:00 p.m.~12:59 p.m.;
24小时制的13:00~23:59是十二小时制的1:00 p.m.~11:59 p.m.。
午夜是“12:00 a.m.”,也就是24小时制的0点,或者24点;
24小时制的00:00~00:59,是12小时制的12:00 a.m.~12:59 a.m.;
24小时制的1:00~11:59是十二小时制的1:00 a.m.~11:59 a.m.。
Input
输入为一行。输入为24小时制的小时和分,都占满2个字符的位置,用“:”分隔。范围是00:00~23:59。
Output
输出为12小时制的小时和分,都占满2个字符的位置,用“:”分隔,一个空格后跟“a.m.”(午前)或“p.m.”(午后)。
Sample Input
21:05
Sample Output
09:05 p.m.
HINT
Append Code
#include <iostream>
using namespace std;
#include <iomanip>int main()
{int a, b;char c;cin >> a >> c >> b;//如何输入中间有个字符的两个整数//占满两个字符(记得前补0)int m;int n;char s1[] = " a.m.";char s2[] = " p.m.";char s[5];if(a==24 && b==00){m = 12;n = 00;cout << setfill('0') << setw(2) << m << ':' << setfill('0') << setw(2) << n << s1 << endl;}else if (a==00){m = 12;n = b;cout << setfill('0') << setw(2) << m << ':' << setfill('0') << setw(2) << n << s1 << endl;}else if (a >= 01 && a <= 11){m = a;n = b;cout << setfill('0') << setw(2) << m << ':' << setfill('0') << setw(2) << n << s1 << endl;}else if (a == 12){m = a;n = b;cout << setfill('0') << setw(2) << m << ':' << setfill('0') << setw(2) << n << s2 << endl;}else if(a >= 13 && a <= 23){m = a - 12;n = b;cout << setfill('0') << setw(2) << m << ':' << setfill('0') << setw(2) << n << s2 << endl;}return 0;
}
Problem D: 应该付多少钱?
Time Limit: 1 Sec Memory Limit: 2 MB
Submit: 7799 Solved: 4246
[Submit][Status]
Description
周末,Tom、Mary、Jack一家三口去逛商场,每人都选购了一些物品。现在,他们推着购物车来到了收银台,你需要编程给出他们需要付多少钱。
Input
输入为多行,每行输入与一件商品对应:第一个实数是该商品的单价,第二个实数是该商品的数量。
Output
所购商品的总价。输出结果精确到分。
Sample Input
25.33 15.3 33.54 22.11 12.12 36.37
Sample Output
1569.92
HINT
需要输出所有的小数吗?
Append Code
#include <iostream>
using namespace std;
#include <iomanip>
#include <signal.h>
int main()
{double a, b;double sum=0.0;while (cin >> a >> b){sum += a * b;}cout << fixed << setprecision(2) << sum << endl;return 0;
}
Problem E: 默认参数:求圆面积
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 8782 Solved: 6983
[Submit][Status]
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
HINT
Append Code
int main()
{double r;cin>>r;cout<<area(r)<<endl;cout<<area()<<endl;return 0;
}
Problem F: 类的初体验
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 1226 Solved: 859
[Submit][Status]
Description
编写一个People类,有如下成员:
class People
{
private:
int age;//年龄
char sex;//性别,用F表示女,M表示男。
public:
void setAge(int a);//设置年龄
void setSex(char b);//设置性别
void printInfo();//显示信息
};
printInfo显示个人信息的格式为:
age:?,sex:?
其中第一个?处用实际年龄代替,第二个?处用实际性别代替。
Input
输入两个人的年龄和性别。
Output
按照题目描述中printInfo输出信息的格式,分别输出两个人的信息,每个人的信息占一行。
Sample Input
12 M 24 F
Sample Output
age:12,sex:M age:24,sex:F
HINT
Append Code
int main()
{People Tom, Mary;int ageTom,ageMary;char sexTom,sexMary;cin>>ageTom>>sexTom;cin>>ageMary>>sexMary;Tom.setAge(ageTom);Mary.setAge(ageMary);Tom.setSex(sexTom);Mary.setSex(sexMary);Tom.printInfo();Mary.printInfo();return 0;
}
#include <iostream>
using namespace std;
#include <iomanip>
#include <signal.h>
#include <string>class People
{
private:int age;//年龄char sex;//性别,用F表示女,M表示男。
public:void setAge(int a){age = a;}//设置年龄void setSex(char b)//设置性别{sex = b;}void printInfo() {cout << "age:" << age << ",sex:" << sex << endl;}
};
Problem G: 重载函数min()
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 522 Solved: 288
[Submit][Status]
Description
编写重载函数min(),计算int、double类型数组中的最小数。
Input
输入相应数组元素
Output
输出对应数组元素的最小数
Sample Input
5 4 3 2 1 88.5 56.2 8.6 1.5 94.6
Sample Output
int min is:1 double min is:1.5
HINT
Append Code
int main()
{int a[5];double b[5];for (int i = 0; i < 5; i++)cin >> a[i];cout << "int min is:" << min(a) << endl;for (int i = 0; i < 5; i++)cin >>b[i];cout << "double min is:"<< min(b)<<endl;return 0;}
#include <iostream>
using namespace std;
#include <iomanip>
#include <signal.h>int min(int arr[])
{for (int i = 0; i < 5; i++){for (int j = 0; j < 5 - 1 - i; j++){if (arr[j] > arr[j + 1]){int t = arr[j];arr[j] = arr[j + 1];arr[j + 1] = t;}}}return arr[0];}
double min(double arr[])
{for (int i = 0; i < 5; i++){for (int j = 0; j < 5 - 1 - i; j++){if (arr[j] > arr[j + 1]){double t = arr[j];arr[j] = arr[j + 1];arr[j + 1] = t;}}}return arr[0];}
Problem H: 函数重载:求面积
Time Limit: 1 Sec Memory Limit: 2 MB
Submit: 863 Solved: 645
[Submit][Status]
Description
编写两个名为area的函数,它们是重载函数 ,用于求圆的面积和长方形的面积。它们的原型分别是:
double area(double r);
double area(double a,double b);
返回值分别是圆的面积和长方形的面积。
Input
输入两行,第一行是一个double型的实数,表示圆的半径;第二行是两个double型的实数,表示长方形的长和宽。
Output
输出2个数,每个数占一行。第一个数表示圆的面积,第二个数表示长方形的面积。
Sample Input
1.2 1.2 3.4
Sample Output
4.5216 4.08
HINT
Append Code
int main()
{double r,x,y;cin>>r;cin>>x>>y;cout<<area(r)<<endl;cout<<area(x,y)<<endl;
}
#include <iostream>
using namespace std;
#include <iomanip>
#include <signal.h>#define pi 3.14
double area(double r)
{return pi * r * r;
}double area(double a, double b)
{return a * b;
}
Problem I: C++的初体验
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 1576 Solved: 841
[Submit][Status]
Description
通过cin,cout进行C++程序的输入和输出
Input
1.输入一个整数a
2.输入一个浮点数b
3.输入一个长整型数c
4.输入一个整数d
Output
第一行输出整数a
第二行输出浮点数b,保留小数点后7位
第三行输出长整型数c
第四行输出整数d,宽度为16,右对齐
Sample Input
1 2.323 2314213435435543 213
Sample Output
1 2.3230000 2314213435435543 213
HINT
Append Code
#include <iostream>
using namespace std;
#include <iomanip>
#include <signal.h>#define pi 3.14int main()
{int a;double b;long long c;int d;cin >> a >> b >> c >> d;cout << a << endl;cout << fixed << setprecision(7) << b << endl;cout << c << endl;cout << right << setw(16) << d << endl;return 0;
}
Problem J: string的初体验
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 2449 Solved: 708
[Submit][Status]
Description
练习string
Input
两个字符串 s1,s2 10<=|S1|,|S2|<=10000;
Output
第一行输出s1和s2的拼接字符串,即s1+s2
第二行输出s1,s2两者中字典序较小者
第三行输出s1的从头开始长度为4的子串
Sample Input
abcdesad dsadsad
Sample Output
abcdesaddsadsad abcdesad abcd
HINT
Append Code
#include <iostream>
using namespace std;
#include <iomanip>
#include <signal.h>
#include <string>int main()
{string s1, s2;//当输入的字符串包含空格时,cin >>无法正确读取整个字符串。// 应改用getline来读取整行输入。getline(cin, s1);getline(cin, s2);string s3 = s1;s3 += s2;cout << s3 << endl;if (s1.compare(s2) >= 0)cout << s2 << endl;elsecout << s1 << endl;cout << s1.substr(0, 4) << endl;return 0;
}
CPP Getline