AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

AcWing 4723. Java 找规律249ms    原题链接    中等

作者: 作者的头像   henrychenOutlook ,  2022-11-26 21:03:11 ,  所有人可见 ,  阅读 121


1


以小数暴力运行test() 会发现注释中的规律 每次以1 2 4 8 16… 的次数pop

所以可以算出每层的[l, r]
1 -> [1, 5]
2 -> [6, 15]
4 -> [16, 35]
8 -> [36, 75]
16 -> [76, 155]
......

找出n在第几层就行

import java.util.*;
import java.io.*;

class Main {
    static PrintWriter pw;

    void solve(int n) {
        if (n == 1) {
            pr('a');
            return;
        }
        char[] a = {'a', 'b', 'c', 'd', 'e'};
        int l = 1;
        for (int i = 0; 1 << i < n; i++) {
            int v = 1 << i, line = v * 5; // v 表示1 2 4 8 16...  line 表示当前层的总数
            int r = l + line - 1;
            int[] range = new int[]{l, r}; // 当前层的range  [1, 5], [6, 15]....
            if (n >= l && n <= r) { // n 在当前层, 计算结果
                double d = (double) (n - l + 1) / v;
                int idx = (int) Math.ceil(d);
                pr(a[idx - 1]);
                break;
            }
            l = r + 1;
        }
    }

    /*
     1 a b c d e
     2 aa bb cc dd ee
     4 aaaa bbbb cccc dddd eeee
     8 aaaaaaaa bbbbbbbb cccccccc dddddddd eeeeeeee
     */
    void test(int n) {
        Deque<Character> q = new ArrayDeque<>();
        q.add('a');
        q.add('b');
        q.add('c');
        q.add('d');
        q.add('e');
        int cnt = 0;
        while (true) {
            char x = q.poll();
            tr(x);
            cnt++;
            if (cnt == n) {
                pr(x);
                break;
            }
            q.add(x);
            q.add(x);
        }
    }

    private void run() {
        // read_write_file(); // comment this before submission
        FastScanner fs = new FastScanner();
        int n = fs.nextInt();
        solve(n);
    }

    private final String INPUT = "input.txt";
    private final String OUTPUT = "output.txt";

    void read_write_file() {
        FileInputStream instream = null;
        PrintStream outstream = null;
        try {
            instream = new FileInputStream(INPUT);
            outstream = new PrintStream(new FileOutputStream(OUTPUT));
            System.setIn(instream);
            System.setOut(outstream);
        } catch (Exception e) {
        }
    }

    public static void main(String[] args) {
        pw = new PrintWriter(System.out);
        new Main().run();
        pw.close();
    }

    <T> void pr(T t) {
        pw.println(t);
    }

    class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        String next() {
            while (!st.hasMoreTokens())
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] readArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }
    }

    void tr(Object... o) {
        pw.println(Arrays.deepToString(o));
    }
}

0 评论

你确定删除吗?
1024
x

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息