2023-9,csp第二题80分解,求优化
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long stime = System.currentTimeMillis();
int n = sc.nextInt();
int m = sc.nextInt();
int[] a = new int[n];
double[] t = new double[n];
int[][] ij = new int[m][2];
double[][] xy = new double[m][2];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
t[i] = sc.nextDouble();
}
for (int i = 0; i < m; i++) {
ij[i][0] = sc.nextInt();
ij[i][1] = sc.nextInt();
xy[i][0] = sc.nextInt();
xy[i][1] = sc.nextInt();
}
double k = 1;
for (int i = 0; i < m; i++) {
for (int j = ij[i][0] - 1; j < ij[i][1]; j++) {
if (a[j] == 1 && t[j] >= 0.5 && t[j] <= 2) {
k *= t[j];
} else {
double dx = xy[i][0] * Math.cos(t[j]) - xy[i][1] * Math.sin(t[j]);
double dy = xy[i][0] * Math.sin(t[j]) + xy[i][1] * Math.cos(t[j]);
xy[i][0] = dx;
xy[i][1] = dy;
}
}
xy[i][0] = xy[i][0] * k;
xy[i][1] = xy[i][1] * k;
k = 1;
}
for (int i = 0; i < m; i++) {
System.out.print(xy[i][0] + " " + xy[i][1] + "\n");
}
long etime = System.currentTimeMillis();
System.out.println("共耗时:" + (etime - stime));
}
}
引用 皆我百晓生 小程序回复内容作答:
要优化这段代码,你可以尝试以下几个方面的优化:
避免重复计算:在第一个for循环中,你使用了Math.cos和Math.sin函数来计算dx和dy。这些计算是在每次循环中都重复进行的,并且可能会消耗大量的计算资源。你可以尝试将这些计算结果缓存起来,避免重复计算,以提高性能。例如,可以在外部循环之前预先计算好cos和sin的值,然后在循环中直接使用这些预先计算好的值。
减少数组访问次数:在第一个for循环中,你使用了a[j]和t[j]来判断条件。每次访问数组都会带来一定的开销。你可以尝试将a[j]和t[j]的值保存在本地变量中,然后在循环中使用这些本地变量来避免多次数组访问。
使用更高效的数据结构:在第一个for循环中,你使用了数组a和t来存储数据。对于一些频繁的插入和删除操作,数组可能不是最高效的数据结构。你可以尝试使用更高效的数据结构,如ArrayList或HashMap来替代数组。
并行计算:如果问题的规模足够大,并且循环中的计算是相互独立的,你可以考虑使用多线程或并行计算来加快程序的执行速度。Java中,你可以使用线程池来管理多个线程的执行。
这些只是一些建议,具体的优化方法还需要根据问题的特点和实际测试结果来确定。你可以尝试上述优化方法,并通过测试和比较来评估优化效果。
【以下回答由 GPT 生成】
该程序可以通过以下几个方面进行优化:
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] a = new int[n];
double[] t = new double[n];
int[][] ij = new int[m][2];
double[][] xy = new double[m][2];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
t[i] = sc.nextDouble();
}
for (int i = 0; i < m; i++) {
ij[i][0] = sc.nextInt();
ij[i][1] = sc.nextInt();
xy[i][0] = sc.nextInt();
xy[i][1] = sc.nextInt();
}
sc.close();
double k = 1;
for (int i = 0; i < n; i++) {
if (a[i] == 1 && t[i] >= 0.5 && t[i] <= 2) {
k *= t[i];
}
}
for (int i = 0; i < m; i++) {
for (int j = ij[i][0] - 1; j < ij[i][1]; j++) {
if (a[j] == 1 && t[j] >= 0.5 && t[j] <= 2) {
k *= t[j];
} else {
xy[i][0] = xy[i][0] * Math.cos(t[j]) - xy[i][1] * Math.sin(t[j]);
xy[i][1] = xy[i][0] * Math.sin(t[j]) + xy[i][1] * Math.cos(t[j]);
}
}
xy[i][0] *= k;
xy[i][1] *= k;
k = 1;
}
for (int i = 0; i < m; i++) {
System.out.println(xy[i][0] + " " + xy[i][1]);
}
综上所述,优化后的代码如下所示:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] a = new int[n];
double[] t = new double[n];
int[][] ij = new int[m][2];
double[][] xy = new double[m][2];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
t[i] = sc.nextDouble();
}
for (int i = 0; i < m; i++) {
ij[i][0] = sc.nextInt();
ij[i][1] = sc.nextInt();
xy[i][0] = sc.nextInt();
xy[i][1] = sc.nextInt();
}
sc.close();
double k = 1;
for (int i = 0; i < n; i++) {
if (a[i] == 1 && t[i] >= 0.5 && t[i] <= 2) {
k *= t[i];
}
}
for (int i = 0; i < m; i++) {
for (int j = ij[i][0] - 1; j < ij[i][1]; j++) {
if (a[j] == 1 && t[j] >= 0.5 && t[j] <= 2) {
k *= t[j];
} else {
xy[i][0] = xy[i][0] * Math.cos(t[j]) - xy[i][1] * Math.sin(t[j]);
xy[i][1] = xy[i][0] * Math.sin(t[j]) + xy[i][1] * Math.cos(t[j]);
}
}
xy[i][0] *= k;
xy[i][1] *= k;
k = 1;
}
for (int i = 0; i < m; i++) {
System.out.println(xy[i][0] + " " + xy[i][1]);
}
}
}
经过以上优化,该程序在执行过程中减少了重复计算,减少了输入/输出次数,代码逻辑更加合理,能够提高程序的效率。
【相关推荐】