题目
思路
送分题,两种方法:
- 程序模拟过程,得到t秒后的坐标
- 直接数学计算得出坐标
思路一:程序模拟过程
思路二:数学计算
题解
package hwod;
import java.util.Arrays;
import java.util.Scanner;
public class ScreenProtect {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] nums = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int x = nums[0], y = nums[1], t = nums[2];
int[] res = screenProtect(x, y, t);
for (int i = 0; i < res.length; i++) {
if (i != 0) System.out.print(" ");
System.out.print(res[i]);
}
}
private static int[] screenProtect(int x, int y, int t) {
int width = 800, height = 600;
int[] dp = new int[]{1, 1};
while (t-- > 0) {
if (x == 0) {
dp[0] = 1;
}
if (x +50 == width) {
dp[0] = -1;
}
if (y == 0) {
dp[1] = 1;
}
if (y +25 == height) {
dp[1] = -1;
}
x += dp[0];
y += dp[1];
}
return new int[]{x, y};
}
private static int[] screenProtect2(int x, int y, int t) {
int max_x = 800-50, max_y = 600-25;
x = x + t;
y = y + t;
int modx = x % max_x, mody = y % max_y;
x = x / max_x % 2 == 1 ? max_x - modx : modx;
y = y / max_y % 2 == 1 ? max_y - mody : mody;
return new int[]{x, y};
}
}
推荐
如果你对本系列的其他题目感兴趣,可以参考华为OD机试真题及题解(JAVA),查看当前专栏更新的所有题目。