CCF coordinate transformation (Part 2) (Java problem solution)

1. Title

Question link: ccf official website

2. Solution (Java version)

At first I only got 80 points using brute force, but later I found out that I could do it with prefixes and got full marks.

import java.util.Scanner;

//用前缀和算法
public class Main {

	static double rotateX(double x, double y, double angle) {
		return x*Math.cos(angle) - y*Math.sin(angle);
	}

	static double rotateY(double x, double y, double angle) {
		return x*Math.sin(angle) + y*Math.cos(angle);
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int m = in.nextInt();
		double[][] d = new double[n+1][2];
		d[0][0] = 1L;
		d[0][1] = 1L;
		for (int i = 1; i <= n; i++) {
			int dx = in.nextInt();
			double dy = in.nextDouble();
			if(dx==1) {   //如果是拉伸,放到索引为0的位置
				d[i][0] = dy;
			}else {    //如果是旋转,放到索引为1
				d[i][1] = dy;
			}
		}
		for (int i = 1; i <= n; i++) {
			if(d[i][0]!=0.0) {  //处理避免与0相乘时的情况
				d[i][0] *= d[i-1][0];
			}else {
				d[i][0] = d[i-1][0];
			}
			d[i][1] += d[i-1][1];
		}
		for (int k = 0; k < m; k++) {
			int i = in.nextInt();
			int j = in.nextInt();
			double x= in.nextDouble();   //坐标
			double y = in.nextDouble();
			x *= d[j][0]/d[i-1][0];
			y *= d[j][0]/d[i-1][0];

			double xi = x;
			double yi = y;
			x = rotateX(xi, yi, d[j][1] - d[i-1][1]); 
			y = rotateY(xi, yi, d[j][1] - d[i-1][1]);
			System.out.println(x+ " "+y);
		}
	}
}

Guess you like

Origin blog.csdn.net/m0_63080216/article/details/133717500