文章目录
- 题目描述
- 输入描述
- 输出描述
- 用例
- 题目解析
- js算法源码
- Java算法源码
- python算法源码
- c算法源码
- c++算法源码
题目描述
绘图机器的绘图笔初始位置在原点(0,0)机器启动后按照以下规则来进行绘制直线.
- 尝试沿着横线坐标正向绘制直线直到给定的终点 E
- 期间可以通过指令在纵坐标轴方向进行偏移,offsetY 为正数表示正向偏移.为负数表示负向偏移给定的横坐标终点值 E 以及若干条绘制指令请计算绘制的直线和横坐标轴以及 X=E 的直线组成的图形面积
输入描述
首行为两个整数 N 和 E
- 表示有 N 条指令.机器运行的横坐标终点值 E
接下来 N 行 每行两个整数表示一条绘制指令 x offsetY
用例保证横坐标 x 以递增排序的方式出现
目不会出现相同横坐标 x
取值范围
- 0<N<=10000
- 0<=X<=E<=20000
- -10000<=offsetY<=10000
输出描述
一个整数表示计算得到的面积 用例保证结果范围在 0 到 4294967295 之内
用例
输入
4 10
1 1
2 1
3 1
4 -2
输出
12
说明
无
输入
2 4
0 1
2 -2
输出
4
说明
无
题目解析
解决思路
- 初始化:设置初始位置为 (0, 0)。
- 处理指令:遍历每个指令,更新当前的纵坐标偏移量,并计算每个小矩形的面积。
- 计算面积:将每个小矩形的面积累加起来,得到总面积。
具体步骤
- 读取输入:读取指令数量 N和横坐标终点值 E,以及 N 条指令。
- 初始化变量:设置初始位置 (0, 0) 和当前纵坐标偏移量为 0。
- 处理指令:遍历每个指令,更新当前纵坐标偏移量,并计算每个小矩形的面积。
- 计算总面积:将每个小矩形的面积累加起来,得到总面积。
js算法源码
function calculateArea(N, E, instructions) {let currentY = 0;let totalArea = 0;let prevX = 0;for (const [x, offsetY] of instructions) {// 计算当前小矩形的面积let area = (x - prevX) * Math.abs(currentY);totalArea += area;// 更新当前纵坐标偏移量currentY += offsetY;prevX = x;}// 计算最后一个矩形的面积let area = (E - prevX) * Math.abs(currentY);totalArea += area;return totalArea;
}// 测试
const N = 3;
const E = 10;
const instructions = [[2, 3], [5, -2], [8, 4]];
console.log(calculateArea(N, E, instructions)); // 输出: 36
Java算法源码
import java.util.Scanner;public class Main {public static long calculateArea(int N, int E, int[][] instructions) {long currentY = 0;long totalArea = 0;int prevX = 0;for (int[] instruction : instructions) {int x = instruction[0];int offsetY = instruction[1];// 计算当前小矩形的面积long area = (long) (x - prevX) * Math.abs(currentY);totalArea += area;// 更新当前纵坐标偏移量currentY += offsetY;prevX = x;}// 计算最后一个矩形的面积long area = (long) (E - prevX) * Math.abs(currentY);totalArea += area;return totalArea;}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int N = scanner.nextInt();int E = scanner.nextInt();int[][] instructions = new int[N][2];for (int i = 0; i < N; i++) {instructions[i][0] = scanner.nextInt();instructions[i][1] = scanner.nextInt();}System.out.println(calculateArea(N, E, instructions));}
}
python算法源码
def calculate_area(N, E, instructions):current_y = 0total_area = 0prev_x = 0for x, offsetY in instructions:# 计算当前小矩形的面积area = (x - prev_x) * abs(current_y)total_area += area# 更新当前纵坐标偏移量current_y += offsetYprev_x = x# 计算最后一个矩形的面积area = (E - prev_x) * abs(current_y)total_area += areareturn total_area# 测试
N = 3
E = 10
instructions = [(2, 3), (5, -2), (8, 4)]
print(calculate_area(N, E, instructions)) # 输出: 36
c算法源码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>long long calculateArea(int N, int E, int instructions[][2]) {long long currentY = 0;long long totalArea = 0;int prevX = 0;for (int i = 0; i < N; i++) {int x = instructions[i][0];int offsetY = instructions[i][1];// 计算当前小矩形的面积long long area = (long long) (x - prevX) * llabs(currentY);totalArea += area;// 更新当前纵坐标偏移量currentY += offsetY;prevX = x;}// 计算最后一个矩形的面积long long area = (long long) (E - prevX) * llabs(currentY);totalArea += area;return totalArea;
}int main() {int N, E;scanf("%d %d", &N, &E);int instructions[N][2];for (int i = 0; i < N; i++) {scanf("%d %d", &instructions[i][0], &instructions[i][1]);}printf("%lld\n", calculateArea(N, E, instructions));return 0;
}
c++算法源码
#include <iostream>
#include <vector>
#include <cmath>long long calculateArea(int N, int E, const std::vector<std::pair<int, int>>& instructions) {long long currentY = 0;long long totalArea = 0;int prevX = 0;for (const auto& instruction : instructions) {int x = instruction.first;int offsetY = instruction.second;// 计算当前小矩形的面积long long area = (long long) (x - prevX) * std::abs(currentY);totalArea += area;// 更新当前纵坐标偏移量currentY += offsetY;prevX = x;}// 计算最后一个矩形的面积long long area = (long long) (E - prevX) * std::abs(currentY);totalArea += area;return totalArea;
}int main() {int N, E;std::cin >> N >> E;std::vector<std::pair<int, int>> instructions(N);for (int i = 0; i < N; i++) {std::cin >> instructions[i].first >> instructions[i].second;}std::cout << calculateArea(N, E, instructions) << std::endl;return 0;
}