PTA第三次上机
5-1
#include <iostream>
#include <cstdlib>
#include <string.h>
using namespace std;
class polygon
{
protected:
int number;
private:
int side_length[100];
public:
polygon(int a = 0):number(a){memset(side_length, 0, sizeof(side_length));};
int perimeter();//计算多边形边长
void display();//输出多边形边数和周长
int* reachsidelen();
};
int polygon::perimeter()
{
int i;
int tot = 0;
for (i = 1; i <= number; i++)
{
tot += side_length[i];
}
return tot;
}
void polygon::display()
{
cout << number << " " << perimeter() << endl;
}
int* polygon::reachsidelen()
{
return side_length;
}
class rectangle : protected polygon
{
protected:
int height;
int width;
public:
rectangle(int a = 0, int b = 0, int c = 0):polygon(a), height(b), width(c){};
int perimeter();//计算矩形边长
void display();//输出多边形边数和周长
};
int rectangle::perimeter()
{
int tot = 0;
tot = height * 2 + width * 2;
return tot;
}
void rectangle::display()
{
cout << number << " " << perimeter() << endl;
}
class equal_polygon : protected polygon
{
protected:
int side_len;
public:
equal_polygon(int a = 0, int b = 0):polygon(a), side_len(b){};
int perimeter();//计算等边多边形边长
void display();//输出多边形边数和周长
};
int equal_polygon::perimeter()
{
int tot = 0;
tot = number * side_len;
return tot;
}
void equal_polygon::display()
{
cout << number << " " << perimeter() << endl;
}
int main()
{
int b[105];
int i,j;
int cnt = 0;
cin >> cnt;
int ope;
while(cnt --)
{
memset(b, 0, sizeof(b));
cin >> ope;
if(ope == 0)
{
int tot = 1;
int idata;
while(cin >> idata)
{
if(idata == -1)break;
b[tot++] = idata;
}
tot --;
polygon P(tot);
int *p = P.reachsidelen();
for (i = 1; i <= tot; i++)
{
*(p + i) = b[i];
}
P.display();
}
else if(ope == 1)
{
int iwide, ilen;
cin >> iwide >> ilen;
rectangle R(4, ilen, iwide);
R.display();
}
else if(ope == 2)
{
int inumber, ilen;
cin >> inumber >> ilen;
equal_polygon E(inumber, ilen);
E.display();
}
}
return 0;
}
5-2
#include <iostream>
#include <stdlib.h>
#include <cmath>
using namespace std;
class Point_1D
{
protected:
float x;//1D 点的x坐标
public:
Point_1D(float a = 0): x(a){};
float distance( );//计算当前点到原点的距离
};
float Point_1D::distance()
{
float d = x;
if(d < 0) d = -d;
return d;
}
class Point_2D : protected Point_1D
{
protected:
float y;
public:
Point_2D(float a = 0, float b = 0): Point_1D(a), y(b){};
float distance( );
};
float Point_2D::distance()
{
float d;
d = sqrt(x * x + y * y);
return d;
}
class Point_3D : protected Point_2D
{
protected:
float z;
public:
Point_3D(float a = 0, float b = 0, float c = 0): Point_2D(a, b), z(c){};
float distance( );
};
float Point_3D::distance()
{
float d;
d = sqrt(x * x + y * y + z * z);
return d;
}
int main()
{
int ope;
while(cin >> ope)
{
if(ope == 0)break;
if(ope == 1)
{
float ix;
cin >> ix;
Point_1D p1(ix);
cout << "Distance from Point " << ix << " to original point is "
<< p1.distance() << endl;
}
else if(ope == 2)
{
float ix, iy;
cin >> ix >> iy;
Point_2D p2(ix, iy);
cout << "Distance from Point(" << ix << "," << iy
<< ") to original point is " << p2.distance() << endl;;
}
else if(ope == 3)
{
float ix, iy, iz;
cin >> ix >> iy >> iz;
Point_3D p3(ix, iy, iz);
cout << "Distance from Point(" << ix << "," << iy << "," << iz
<< ") to original point is " << p3.distance() << endl;;
}
}
return 0;
}
5-3
用了sstream,主函数太长了。。
#include <iostream>
#include <cstdlib>
#include <sstream>
using namespace std;
class Date
{
protected:
int year;
int month;
int day;
public:
Date(int a = 0, int b = 0, int c = 0):year(a), month(b), day(c){};
};
class Time
{
protected:
int hour;
int minute;
int second;
public:
Time(int a = 0, int b = 0, int c = 0):hour(a), minute(b), second(c){};
};
class Schedule : protected Date, protected Time
{
protected:
int ID;
public:
Schedule(int y = 10000, int m = 0, int d = 0, int h = 0, int min = 0, int s = 0, int id = 0):
Date(y, m, d), Time(h, min, s), ID(id){};
bool operator < (const Schedule & s2);
void display();
};
void Schedule::display() //The urgent schedule is No.1: 2014/6/27 8:0:1
{
cout << ID << ": " << year << "/" << month << "/" << day << " " << hour
<< ":" << minute << ":" << second << endl;
}
bool Schedule::operator < (const Schedule & s2)
{
int dtot1 = 0, tot1 = 0;
int dtot2 = 0, tot2 = 0;
dtot1 = year * 365 + month * 30 + day;
dtot2 = s2.year * 365 + s2.month * 30 + s2.day;
tot1 = hour * 3600 + minute * 60 + second;
tot2 = s2.hour * 3600 + s2.minute * 60 + s2.second;
if(dtot1 < dtot2)return true;
else if(dtot1 > dtot2)return false;
else
{
if(tot1 < tot2)return true;
else return false;
}
}
int main()
{
int id;
int i;
int record;
Schedule sch;
while(cin >> id)
{
if(id == 0)break;
string date, time;
cin >> date >> time;
stringstream stream;
string s1, s2;
int y = 0, m = 0, d = 0, h = 0, min = 0, s = 0;
bool flag = false;
for(i = 0; i < date.length(); i++)
{
if(date[i] == '/')
{
if(flag)
{
stream << s1;
stream >> m;
stream.clear();
s1 = "";
}
else
{
stream << s1;
stream >> y;
stream.clear();
s1 = "";
flag = true;
}
}
else
s1 += date[i];
}
stream << s1;
stream >> d;
stream.clear();
bool flag2 = false;
for(i = 0; i < time.length(); i++)
{
if(time[i] == ':')
{
if(flag2)
{
stream << s2;
stream >> min;
stream.clear();
s2 = "";
}
else
{
stream << s2;
stream >> h;
stream.clear();
s2 = "";
flag2 = true;
}
}
else
s2 += time[i];
}
stream << s2;
stream >> s;
stream.clear();
Schedule sch1(y, m, d, h, min, s, id);
if(sch1 < sch)sch = sch1;
}
cout << "The urgent schedule is No.";
sch.display();
return 0;
}
PTA第三次上机的更多相关文章
- 17秋 SDN课程 第三次上机作业
SDN 第三次上机作业 1.创建拓扑 2.利用OVS命令下发流表,实现vlan功能 3.利用OVS命令查看流表 s1: s2: 4.验证性测试 5.Wireshark 抓包验证
- SDN 第三次上机作业
SDN 第三次上机作业 1.创建拓扑 2.利用OVS命令下发流表,实现vlan功能 3.利用OVS命令查看流表 s1: s2: 4.验证性测试 5.Wireshark 抓包验证
- 『嗨威说』算法设计与分析 - PTA 数字三角形 / 最大子段和 / 编辑距离问题(第三章上机实践报告)
本文索引目录: 一.PTA实验报告题1 : 数字三角形 1.1 实践题目 1.2 问题描述 1.3 算法描述 1.4 算法时间及空间复杂度分析 二.PTA实验报告题2 : 最大子段和 2.1 实践题目 ...
- Java第三次上机随笔
就记录一下新的收获吧~ 1.定点数(BigDecimal) 先区分一下浮点数和定点数: 浮点数(float/double):小数点可以任意浮动,称为浮点表示法 定点数(BigDecimal):一种数约 ...
- PTA第三次作业
---恢复内容开始--- 题目 7-1 计算职工工资 1.设计思路 (1)第一步:观察题意了解各个参数与所需函数在题目中的意义: 第二步:设计算法编写函数,让函数的功能实现题目中所需的功能: 第三步: ...
- PTA第三个编程题总结
7-1 抓老鼠啊~亏了还是赚了? (20 分) 某地老鼠成灾,现悬赏抓老鼠,每抓到一只奖励10元,于是开始跟老鼠斗智斗勇:每天在墙角可选择以下三个操作:放置一个带有一块奶酪的捕鼠夹(T),或者放置一块 ...
- PTA寒假三
抓老鼠啊~亏了还是赚了? (20 分) 某地老鼠成灾,现悬赏抓老鼠,每抓到一只奖励10元,于是开始跟老鼠斗智斗勇:每天在墙角可选择以下三个操作:放置一个带有一块奶酪的捕鼠夹(T),或者放置一块奶酪(C ...
- 2016级算法第三次上机-G.Winter is coming
904 Winter is coming 思路 难题.首先简化问题, \(n\) 个0与 \(m\) 个1排成一列,连续的0不能超过x个,连续的1不能超过y个,求排列方法数. 显然会想到这是动态规划. ...
- 2016级算法第三次上机-C.AlvinZH的奇幻猜想——三次方
905 AlvinZH的奇幻猜想--三次方 思路 中等题.题意简单,题目说得简单,把一个数分成多个立方数的和,问最小立方数个数. 脑子转得快的马上想到贪心,从最近的三次方数往下减,反正有1^3在最后撑 ...
随机推荐
- 优化dedecms设置文章url自定义规则
DEDECMS自定义URL规则的做得还是不错的,可清楚的看到URL中可用到的变量,并且这些变量包括年月日.时间戳.文章 ID.拼音+文章ID.拼音部首.栏目目录及日期加ID转换的字符串等,基本可以很好 ...
- abap 基本知识
sap gui 安装教程:http://www.itpub.net/forum.php?mod=viewthread&tid=2090890 1:abap 基本数据类型: i(整型),f(浮点 ...
- Linux实验楼学习之二
新建文件1-1.c touch 1-1.c 编辑文件1-1.c gedit 1-1.c 生成可执行文件1-1.c gcc -o 1-1 1-1.c 执行可执行文件1-1 ./1-1
- rsync 配置详解
安装 [root@localhost ~]# yum install -y rsync [root@localhost ~]# systemctl start rsyncd [root@localho ...
- ComBSTR的使用
用 CComBSTR 进行编程 Visual Studio .NET 2003 3(共 3)对本文的评价是有帮助 - 评价此主题 ATL 类 CComBSTR 提供对 BSTR 数据类型的包装 ...
- Summary: Binary Search
Iterative ways: int binarySearch (int[] a, int x) { int low = 0; int high = a.length - 1; int mid; w ...
- Linux系统——MySQL基础(二)
# MySQL数据库完全备份与恢复## 数据库备份的分类1. 从物理与逻辑的角度,备份可以分为物理备份和逻辑备份.(1)物理备份:对数据库操作系统的物理文件(数据文件.日志文件)的备份.物理备份又可分 ...
- [转]C语言四书五经
我们来说说C语言方面的图书.什么,C语言?有读者奇怪了.没错,这一次的主角就是诞生于1973年如今已经儿孙满堂的C语言.我们之所以要谈及C,不仅仅是因为它的影响深远,这完全可以从C系列语言家族的兴旺发 ...
- 015-awk
1.awk的处理方式: 一次处理一行. 对每行可以进行切片处理,针对字段处理.2.awk的格式: awk BEGIN{循环之前初始化} [options] 'command' END{循环之后结尾} ...
- cxf-webservice完整示例
最近一段时间研究webservice,一般来说,开发java的Webservice经常使用axis2和cxf这两个比较流行的框架 先使用cxf,开发一个完整示例,方便对webservice有一个整体的 ...