第二题有什么简单的思路吗
点赞 4

相关推荐

第二题给你一个坐标x,y。问从0,0像素点的左下角到x,y像素点的右上角经过多少个像素点。大家做了几道题```javaimport java.util.Scanner;// 注意类名必须为 Main, 不要有任何 package xxx 信息public class Main {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        // 注意 hasNext 和 hasNextLine 的区别        int x = in.nextInt();        int y = in.nextInt();        // RuntimeException runtimeException = new RuntimeException(x+" "+y);        // throw runtimeException;        if (x == y) {            System.out.println(x + 1);            return;        }        if (y > x) {            int temp = x;            x = y;            y = temp;        }        int gcd = get(x + 1, y + 1);        if (gcd == 1) {            System.out.println(x + y + 1);        } else {            int temp = (x + 1) / gcd - 1 + (y + 1) / gcd - 1 + 1;            System.out.println(temp * gcd);        }    }    static int get(int x, int y) {        if (y == 0) {            return x;        }        return get(y, x % y);    }}```
查看1道真题和解析 投递网易雷火等公司10个岗位
点赞 评论 收藏
分享
牛客网
牛客企业服务