我们在做算法题目过程中,可能会遇见需要检测多个样例,但是每一次都关掉程序再重新打开运行的话速度就会很慢,所以我们通过下面这个特殊代码,实现快速检测多个样例
看例题
住宅楼类
建立基类Building,作为楼房类,这个基类中包含楼房层数、房间数、楼房总面积数。
再建立派生类Home_Arch作为住宅楼类。在类Home_Arch中包含的内容有卧室数、客厅数、卫生间数和厨房数等。
在main()中定义一个派生类的对象,并输出信息到屏幕上。
描述
在此处编写题目描述信息
点击编辑器菜单栏按钮 {;} 添加示例代码
输入输出示例
在此处提供一个符合格式描述的例子, 对于自动评阅题, 您也可以选择直接在测试用例中提供
输入 | 输出 | |
示例 1 | | |
#include<iostream>
using namespace std;
class Building
{
private:
int floor,room,area;
public:
Building(int floor_,int room_,int area_)
{
floor=floor_;
room=room_;
area=area_;
}
void show()
{
cout<<"住宅楼:"<<endl;
cout<<"楼层层数:"<<floor<<"层"<<endl;
cout<<"房间数:"<<room<<"间"<<endl;
cout<<"楼房总面积:"<<area<<"平方米"<<endl;
}
};
class Home_Arch:public Building//公有继承Building基类
{
private:
int bedroom,livingroom,bathroom,kitchen;
public:
Home_Arch(int floor_,int room_,int area_,int bedroom_,int livingroom_,int bathroom_,int kitchen_):Building(floor_,room_,area_)
{
bedroom=bedroom_;
livingroom=livingroom_;
bathroom=bathroom_;
kitchen=kitchen_;
}
void show1()
{
show();
cout<<"其中:"<<endl;
cout<<"卧室数:"<<bedroom<<"间"<<endl;
cout<<"客厅数:"<<livingroom<<"间"<<endl;
cout<<"卫生间数:"<<bathroom<<"间"<<endl;
cout<<"厨房数:"<<kitchen<<"间"<<endl;
}
};
int main()
{
int floor,room,area,bedroom,livingroom,bathroom,kitchen ;
while((cin>>floor)!=0)//需要输入的7个样例数,只要有输入,这个程序就会不断地执行循环体里面的内容
//在输入第7个数据(中间数据按空格)按回车后,就会显示a.show1()的内容
{
cin>>room>>area>>bedroom>>livingroom>>bathroom>>kitchen;
Home_Arch a(floor,room,area,bedroom,livingroom,bathroom,kitchen);
a.show1();
}//并且这个代码程序是能够提交的, 我猜想的可能解释是因为每个判断题目正确的程序是:
//OJ判题是会把一组input文件放进标准输入流中,然后将输出数据放进output文件中,最后把output文件中的数据与正确答案进行比较
//反正这段巧妙的代码添加在需要检测多个样例的题目里都是可以提交的
}
在按回车后它还可以继续输入
尝试去使用就好了,他能节约时间,多次检查 ,在c语言中类似用法还有
scanf("%d",&n)!=EOF
计算两点间的距离
输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。
输入
输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。
输出
对于每组输入数据,输出一行,结果保留两位小数。
样例输入 Copy
0 0 0 1
0 1 1 0
样例输出 Copy
1.00
1.41
#include<stdio.h>
#include<math.h>
int main()
{
double x1, y1, x2, y2, s;
while (scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2) != EOF)
{
s = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
printf("%.2lf\n", s);
}
return 0;
}//1.EOF是end of file 的缩写,是一个值为 -1 的宏。包括scanf,很多函数在读取完文件后会返回EOF
//所以用scanf读取输入文件,当文件结束就返回EOF,这样我们就能使用 while (scanf("%d", &i) != EOF) 这个循环进行解题
还能够继续输入样例进行测试的,这个也可以作为答案提交
尝试去使用就好了,点到为止,直接去探索吧!