1840: Lawn mower

1840: Lawn mower

      Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 217     Solved: 115    

Description

    The International Collegiate Soccer1 Competition (ICSC) is famous for its well-kept rectangular stadiums. The grass playing fields in ICSC stadiums are always 100 meters long, and 75 meters wide. The grass is mowed every week with special lawn mowers, always using the same strategy: first, they make a series of passes along the length of the field, and then they do the same along the width of the field. All passes are straight lines, parallel to the sides of the field.

    The ICSC has hired a new lawn-mower, Guido. Guido is very chaotic, and instead of covering the field incrementally, he likes to choose random starting positions for each of his passes. But he is afraid of not doing a good job and being red by the ICSC, so he has asked you to help him. Write a program to make sure that the grass in the field is perfectly cut: all parts of the field have to be mowed at least once when the mower goes from end to end, and again when the mower goes from side to side.

Input

  Each test case contains 3 lines. The first line contains two integers, nx (0 < nx < 1 000) and ny(0 < nx < 1 000), and a real number w (0<w50

), which represents the width of the cut of thatparticular lawn mower. The next line describes the end-to-end passes (along the length of the field), andcontains nx real numbers xi ( 0xi75) describing the starting positions of the mower's center in Guido'send-to-end passes. The last line describes the side-to-side passes, with ny real numbers yi ( 0yi100

).

  The end of the test cases is signalled with a line that contains the numbers 0 0 0.0. You should generateno output for this line, as it is not a test case.

  Real numbers for w, xi and yi can have up to 7 digits after the decimal point, and any cut will alsoinclude its boundaries. For example, if a 2.0-meter wide cut is performed along the 10.0-meter mark, thena strip of grass from 9.0 to 11.0 (including both) will be considered "cut".

Output

  Print YES if Guido has done a good job, or NO if some part of the field has not been mowed at leastonce when the mower was travelling along the length of the field, and again when it was travelling along thewidth.

Sample Input

8 11 10.0
0.0 10.0 20.0 30.0 40.0 50.0 60.0 70.0
0.0 10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0
8 10 10.0
0.0 10.0 20.0 30.0 40.0 50.0 60.0 70.0
0.0 10.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0
4 5 20.0
70.0 10.0 30.0 50.0
30.0 10.0 90.0 50.0 70.0
4 5 20.0
60.0 10.0 30.0 50.0
30.0 10.0 90.0 50.0 70.0
0 0 0.0

Sample Output

YES
NO
YES
NO

题目看上去挺吓人的,对,看上去,只是看上去。

题意:在一块75*100的草坪上有一些除草机,横竖都有,告诉了你除草机的台数还有除草机的工作宽度。每台除草机都只能直线前进,问能否将草坪上的草除干净。每组测试用例有三行,第一行是横纵除草机的数量和除草机工作宽度。接下来两行是每台除草机的位置。第二行是横的,第三行是纵的。每台除草机的工作范围是以所给坐标为中心往两边展开。例如除草机位置为20.0,工作宽度为20.0。那么它工作的范围是从10.0到30.0 。

题目所给的除草机位置是没有排序的,所以需要自己进行排序。然后判断边界能否工作到以及相邻两台除草机之间有没有空隙。如果有没讲清楚的地方请看代码。代码有注释,有不懂再问吧

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
double a[1005], b[1005];
int main()
{
	int m, n;
	double w;
	while (cin >> m >> n >> w, m + n + w)
	{
		int flag = 0;
		for (int i = 0; i < m; i++)
		{
			scanf ("%lf", &a[i]);
		}
		for ( int i = 0; i < n; i++)
		{
			scanf ("%lf", &b[i]);
		}
		sort (a, a + m);
		sort (b, b + n);
		if(a[0] > w / 2.0 || 75 - a[m - 1] > w / 2.0)//边界判断
		//a[0]>w/2.0说明a[0]-(w/2.0)>0,说明左边界清不到
		//a[0]是第一台除草机的位置,它的工作范围是a[0]-(w/2.0)到a[0]+(w/2.0)
		{
			cout << "NO\n";
			continue;
		}
		if(b[0] > w / 2.0 || 100 - b[n - 1] > w / 2.0)
		{
			cout << "NO\n";
			continue;
		}
		for (int i = 0; i < m - 1; i++)
		{
			if (a[i + 1] - a[i] > w)//空隙判断
			{
				flag = 1;
				break;
			}
		}
		for (int i = 0; i < n - 1; i++)
		{
			if (b[i + 1] - b[i] > w)
			{
				flag = 1;
				break;
			}
		}
		if (flag)
		{
			cout << "NO\n";
		}
		else
		{
			cout << "YES\n";
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴为您解答这个问题,下面是C++的代码: ```c++ #include <iostream> #include <fstream> #include <iomanip> #include <cstring> using namespace std; // 工具结构体 struct Tool { char name[20]; // 工具名称 int num; // 数量 float price; // 价格 }; // 将工具写入文件 void writeTool(fstream& file, int pos, Tool tool) { file.seekp(pos * sizeof(Tool)); // 定位记录位置 file.write(reinterpret_cast<char*>(&tool), sizeof(Tool)); // 写入数据 } // 读取指定位置的工具 Tool readTool(fstream& file, int pos) { Tool tool; file.seekg(pos * sizeof(Tool)); // 定位记录位置 file.read(reinterpret_cast<char*>(&tool), sizeof(Tool)); // 读取数据 return tool; } // 显示所有工具 void displayAllTools(fstream& file) { Tool tool; file.clear(); // 清空文件状态 file.seekg(0); // 定位到文件开头 int pos = 0; cout << left << setw(5) << "编号" << setw(20) << "名称" << setw(10) << "数量" << setw(10) << "价格" << endl; while (file.read(reinterpret_cast<char*>(&tool), sizeof(Tool))) { if (strlen(tool.name) > 0) { cout << left << setw(5) << pos << setw(20) << tool.name << setw(10) << tool.num << setw(10) << tool.price << endl; } pos++; } } // 添加一个新工具 void addTool(fstream& file) { Tool tool; cout << "请依次输入工具名称、数量、价格:" << endl; cin >> tool.name >> tool.num >> tool.price; writeTool(file, file.tellp() / sizeof(Tool), tool); // 写入文件 cout << "添加成功!" << endl; } // 根据编号查找工具 int findTool(fstream& file, int pos) { Tool tool = readTool(file, pos); if (strlen(tool.name) == 0) { return -1; // 已删除的记录 } else { return pos; } } // 删除一条工具记录 void deleteTool(fstream& file) { int pos; cout << "请输入要删除的工具编号:" << endl; cin >> pos; pos = findTool(file, pos); if (pos == -1) { cout << "没有找到该工具!" << endl; } else { Tool tool; memset(&tool, 0, sizeof(Tool)); // 将该记录清空 writeTool(file, pos, tool); cout << "删除成功!" << endl; } } // 更新一条工具记录 void updateTool(fstream& file) { int pos; cout << "请输入要更新的工具编号:" << endl; cin >> pos; pos = findTool(file, pos); if (pos == -1) { cout << "没有找到该工具!" << endl; } else { Tool tool = readTool(file, pos); cout << "当前工具信息:" << endl; cout << left << setw(5) << pos << setw(20) << tool.name << setw(10) << tool.num << setw(10) << tool.price << endl; cout << "请依次输入新的工具名称、数量、价格:" << endl; cin >> tool.name >> tool.num >> tool.price; writeTool(file, pos, tool); cout << "更新成功!" << endl; } } // 主函数 int main() { const char* filePath = "hdwm.dat"; fstream file(filePath, ios::in | ios::out | ios::binary); // 打开文件流 if (!file) { cerr << "无法打开文件!" << endl; return 1; } // 初始化文件 Tool tool = {"", 0, 0}; for (int i = 0; i < 100; i++) { writeTool(file, i, tool); } // 插入初始数据 tool = {"Electric sander", 7, 57.98}; writeTool(file, 3, tool); tool = {"Hammer", 76, 11.99}; writeTool(file, 17, tool); tool = {"Jig saw", 21, 11.00}; writeTool(file, 24, tool); tool = {"Lawn mower", 3, 79.50}; writeTool(file, 39, tool); tool = {"Power saw", 18, 99.99}; writeTool(file, 56, tool); tool = {"Screwdriver", 106, 6.99}; writeTool(file, 68, tool); tool = {"Sledge hammer", 0, 21.50}; writeTool(file, 77, tool); tool = {"Wrench", 34, 7.50}; writeTool(file, 83, tool); // 显示菜单 int choice; while (true) { cout << "请选择操作:\n1. 显示所有工具\n2. 添加一个新工具\n3. 删除一条工具记录\n4. 更新一条工具记录\n5. 退出" << endl; cin >> choice; switch (choice) { case 1: displayAllTools(file); break; case 2: addTool(file); break; case 3: deleteTool(file); break; case 4: updateTool(file); break; case 5: file.close(); return 0; default: cout << "无效的选项,请重新输入!" << endl; break; } } } ``` 这个程序实现了初始化文件、插入初始数据、显示所有工具、添加一个新工具、删除一条工具记录和更新一条工具记录等功能。在程序运行时,会显示一个菜单,用户可以根据自己的需求选择相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值