Skip to content

7-6 答题卡

post.JPG

新浪微博上有网友发文称:“朋友买了本玻尔 X 海森堡的物理大佬同人本,送了 300 道高数题。更绝的是,要做完题目按照答案涂答题卡,涂出一个二维码,扫描二维码才能看到特典,做错了就看不到了……”那张传说中的答题卡如下图所示:若答案为 4 位整数(位数不足时在前面补足 0),则前两位为横坐标,后两位为纵坐标。若一题有两小问,则第一问答案为横坐标,第二问答案为纵坐标。若答案为分数,则分子为横坐标,分母为纵坐标。

card.jpg

本题就请你根据答案帮助读者填写答题卡。

输入格式:

输入首先在第一行给出两个正整数:2<n≤90 为二维码的规模,即二维码是由 n×n 个小方块组成的大方块,左下角的小方块对应坐标 (1, 1),右上角的小方块对应坐标 (n, n);另一个 m(<n2)是答案的个数。最后 m 行,每行按以下格式之一给出一题的答案:或者是一个不超过 4 位的整数;或者是两小问的答案 答案1;答案2;或者是一个分数 分子/分母。这里保证每个答案都可以解析为一个二维码中的方块位置(即不存在超出二维码范围的坐标)。

输出格式:

输出 n 行,每行 n 个字符,空格用 . 表示,涂了答案的黑格用 # 表示。

输入样例:

tex
5 7
205
3;2
4/5
101
3;3
4/3
5;1
5 7
205
3;2
4/5
101
3;3
4/3
5;1

输出样例:

tex
.#.#.
.....
..##.
..#..
#...#
.#.#.
.....
..##.
..#..
#...#

Solution:

java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] input = in.readLine().split(" ");
        int n = Integer.parseInt(input[0]);
        int m = Integer.parseInt(input[1]);
        int[][] code = new int[n + 1][n + 1];
        for (int i = 0; i < m; i++) {
            String s = in.readLine();
            if (s.matches("\\d+")) {
                s = String.format("%04d", Integer.parseInt(s));
                int length = s.length() / 2;
                int x = Integer.parseInt(s.substring(0, length));
                int y = Integer.parseInt(s.substring(length));
                code[n + 1 - y][x] = 1;
            } else {
                String[] x = s.split("\\W");
                code[n + 1 - Integer.parseInt(x[1])][Integer.parseInt(x[0])] = 1;
            }
        }
        for (int i = 1; i < code.length; i++) {
            for (int j = 1; j < code[i].length; j++) {
                if (code[i][j] == 1) {
                    System.out.print("#");
                } else {
                    System.out.print(".");
                }
            }
            System.out.println();
        }
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] input = in.readLine().split(" ");
        int n = Integer.parseInt(input[0]);
        int m = Integer.parseInt(input[1]);
        int[][] code = new int[n + 1][n + 1];
        for (int i = 0; i < m; i++) {
            String s = in.readLine();
            if (s.matches("\\d+")) {
                s = String.format("%04d", Integer.parseInt(s));
                int length = s.length() / 2;
                int x = Integer.parseInt(s.substring(0, length));
                int y = Integer.parseInt(s.substring(length));
                code[n + 1 - y][x] = 1;
            } else {
                String[] x = s.split("\\W");
                code[n + 1 - Integer.parseInt(x[1])][Integer.parseInt(x[0])] = 1;
            }
        }
        for (int i = 1; i < code.length; i++) {
            for (int j = 1; j < code[i].length; j++) {
                if (code[i][j] == 1) {
                    System.out.print("#");
                } else {
                    System.out.print(".");
                }
            }
            System.out.println();
        }
    }
}

Released under the MIT License.