Numerical scale segmentation composition (Los Valley P1008, P1618 Interpretations, Java language description)

P1008 questions asked

P1008 topic Link
Here Insert Picture Description

P1618 questions asked

P1618 topic Link
Here Insert Picture Description

analysis

P1618 P1008 is an enhanced version of such a water problem so no water, but still quite simple.

In fact, judge () function, then, two questions can be shared, is to determine what is a "slot is full" only. If there is accounted for on the pit-bit, on such a thought.

main () in the basic process, then there is nothing special algorithm, enumeration violence on the line.
The first question because it is 1: 2: 3, also the lower limit 123, 333 will limit, can traverse the narrow inside.
Since then the second question is A: B: C, upper and lower limits can not be set up from 1 to 999 to limit violence thousand million, but must set limits on A, B, C, where would all be in the 100 to 999 between, this is very important to ensure that the range of data can be avoided RE (array bounds) before the judge ().

P1618 WA for the first time submitted a sample:
Here Insert Picture Description

I get the test data 5:
in
123 456 789
OUT
123 456 789

In fact, the above said problems should not be in circulation restrictions for, but should () before restrictions in judge.

P1008 ~ AC Code (Java description language)

public class Main {

    private static byte[] arr = new byte[9];

    public static void main(String[] args) {
        for (int i = 123; i < 333; i++) {
            arr = new byte[9];
            int two = 2*i, three = 3*i;
            if (judge(i) && judge(two) && judge(three)) {
                System.out.println(i + " " + two + " " + three);
            }
        }
    }

    private static boolean judge(int i) {
        int a = i / 100;
        int b = (i % 100) / 10;
        int c = i - a*100 - b*10;
        if (b == 0 || c == 0 || a == b || a == c || b == c || arr[a-1] == 1 || arr[b-1] == 1 || arr[c-1] == 1) {
            return false;
        }
        arr[a-1] = arr[b-1] = arr[c-1] = 1;
        return true;
    }
    
}

P1618 ~ AC Code (Java description language)

import java.util.Scanner;

public class Main {

    private static byte[] arr = new byte[9];

    private static boolean judge(int i) {
        int a = i / 100;
        int b = (i % 100) / 10;
        int c = i - a*100 - b*10;
        if (b == 0 || c == 0 || a == b || a == c || b == c || arr[a-1] == 1 || arr[b-1] == 1 || arr[c-1] == 1) {
            return false;
        }
        arr[a-1] = arr[b-1] = arr[c-1] = 1;
        return true;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt(), b = scanner.nextInt(), c = scanner.nextInt();
        scanner.close();
        boolean flag = false;
        for (int i = 1; i < 1000; i++) {
            arr = new byte[9];
            int one = a*i, two = b*i, three = c*i;
            if (one > 100 && one < 1000 && two > 100 && two < 1000 && three > 100 && three < 1000
                    && judge(one) && judge(two) && judge(three)) {
                flag = true;
                System.out.println(one + " " + two + " " + three);
            }
        }
        if (!flag) {
            System.out.println("No!!!");
        }
    }

}
Published 364 original articles · won praise 623 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_43896318/article/details/104082508