Java - 如何实现程序的滑动窗口

标签 java java.util.scanner sliding-window

我正在努力解决如何针对以下问题实现滑动窗口

我正在编写一个程序,该程序应该询问文件的位置(其中包含 PI 的数字)以及要通过用户的 Scanner 类解析的数字位数。

问题: 一家公司正在评估是否可以通过解析 PI 的十进制数字后的数字来分配电话号码。假设每个电话号码有 5 位数字。因此,第一个电话号码小数点后的前 5 位数字,从第二个位置开始的 5 位数字形成第二个电话号码(它遵循滑动窗口模式,一次滑动一位数字)。

  • 如果所有电话号码都是唯一的,则程序应打印“任务成功,所有电话号码都是唯一的”。
  • 但是,如果没有,那么它应该打印“电话号码重复”。还应打印生成的电话号码总数。该程序应该能够读取 100,000 位 PI。

以下是我迄今为止的尝试。

滑动窗口如何实现?

非常感谢任何建议。

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;


public class Problem3 {
    public static void main(String[] args) throws Exception {
        FileInputStream inputstream = null;
        BufferedReader reader = null;
        Set<String> set = new HashSet<String>();

        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);

        try {
            System.out.println("Give the location of the file (example: C:\\Users\\Onur\\workspace\\ProgAssign3\\src\\pi.txt):");
            String fileloc = input.nextLine();

            inputstream = new FileInputStream(fileloc);
            reader = new BufferedReader(new InputStreamReader(inputstream));
            String stringinput;

            System.out.println("Number of digits of PI to parse: ");
            int parsenum = input.nextInt() + 2;
            String[] stringarray = new String[parsenum];

            while((stringinput = reader.readLine()) != null) {
                stringinput = stringinput.substring(2, parsenum);

                for(int i = 0; i < stringinput.length(); i++) {
                    stringarray = stringinput.split("");
                }
            }

            for(int i = 0; i < 5; i++){
                set.add(stringarray[i]);
            }

            System.out.println(set);

        } 
        catch (FileNotFoundException exception) {
            System.err.println("File not found, please try again");
            main(null);
        }
        catch (Exception e) {
            System.err.println("Invalid input entered");
        }
        finally {
            reader.close();
        }
    }
}

最佳答案

处理“滑动窗口”的一种方法是使用 RandomAccessFile ,并寻找当前偏移量。

唯一性可以通过 Set 来处理.

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashSet;
import java.util.Set;

public class NumberGeneratorPI {

    private static final int N = 100_000;
    private static final String PI = "pi.txt"; // 1415...
    private static final byte[] buf = new byte[5];

    public static void main(String[] args) throws IOException {
        Set<String> numbers = new HashSet<>();
        RandomAccessFile digits = new RandomAccessFile(new File(PI), "r");
        for (int i = 0; i < N; i++) {
            digits.seek(i);
            digits.read(buf);
            if (!numbers.add(new String(buf)))
                System.out.println("Duplicate!");
        }
        // Do something with numbers
    }
}

关于Java - 如何实现程序的滑动窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30022500/

相关文章:

java - 在不实现 Serializable 或 Parcelable 的情况下在 Intent 中传递对象引用

java - IntelliJ IDE 使用空格键而不是 Tab 自动完成功能时遇到问题

scala - 在 Seq[Person] 中查找人和直接邻居

python - 在 Numpy 数组中匹配模板的最有效方法是什么?

java - 美国 map 的四色定理Java实现

Java 9 - 如何在 Jshell 上查看帮助?

java - 在同一行 JAVA 中获取各种整数输入

Java:如何使用分隔符 "<"或 ">"或 "><"来使用扫描仪读取

java - ArrayList彩票游戏-多余的数组列表

java - 如何使用Java Stream API解决滑动窗口问题?