当前位置: 首页 > news >正文

【嵙大o】C++作业合集

参考:

C++ swap(交换)函数 指针/引用/C++自带-CSDN博客

Problem IDTitle
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 IC++的初体验
2278 Problem Jstring的初体验

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

http://www.xdnf.cn/news/514063.html

相关文章:

  • 不同版本 Linux 系统账号操作指令 ——rtkit 账号删除、普通账号的创建 / 删除 / 权限修改超详细大全
  • 如何在 Windows 11 或 10 上安装 Amazon Corretto
  • Ubuntu 20.04 报错记录: Matplotlib 无法使用 OpenCV 的 libqxcb.so
  • O2O电商变现:线上线下相互导流——基于定制开发开源AI智能名片S2B2C商城小程序的研究
  • Python蓝色飘雪
  • Linux云计算训练营笔记day10(MySQL数据库)
  • Java虚拟机 - JVM与Java体系结构
  • MyBatis 核心技术详解:从连接池到多表查询
  • Python多进程、多线程、协程典型示例解析
  • 深入理解 OpenCV 的 DNN 模块:从基础到实践
  • OpenSearch入门:从文档示例到查询实战
  • MCP - Cline 接入 高德地图 Server
  • DAY 29 复习日:类的装饰器
  • # 终端执行 java -jar example.jar 时(example.jar为项目jar包)报错:“没有主清单属性” 的解决方法
  • 第一章:重启之始
  • 零基础搭建!基于PP-ShiTuV2的轻量级图像识别系统(Docker+API部署指南)
  • 蓝桥杯1140 最小质因子之和(Hard Version)
  • 2KW压缩机驱动参考设计【SCH篇】
  • 使用conda创建python虚拟环境,并自定义路径
  • C++学习:六个月从基础到就业——C++20:协程(Coroutines)
  • Golang内存逃逸
  • 用代码解读_AI_强化学习在机器人路径规划中的应用与优化
  • nginx相关面试题30道
  • OpenCV-去噪效果和评估指标方法
  • MapReduce-WordCount实现按照value降序排序、字符小写、识别不同标点
  • 【ROS2】 核心概念6——通信接口语法(Interfaces)
  • 定时器相关概念
  • C++(243~263)STL常用算法、遍历算法(for_each,Transform)、查找算法、拷贝和替换、常用算术生成,常用集合算法。
  • 2025抓包工具Reqable手机抓包HTTPS亲测简单好用-快速跑通
  • 小米汽车:新能源赛道的破局者与变革者