【问题标题】:Java - How do I implement sliding window for program [closed]Java - 如何为程序实现滑动窗口[关闭]
【发布时间】:2015-07-13 09:19:18
【问题描述】:

我正在努力解决以下问题的滑动窗口

我正在编写一个程序,该程序应询问文件的位置(其中包含 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();
        }
    }
}

【问题讨论】:

  • 反正我看不出与 pi 有什么关系。您只是从一个充满数字的文本文件中读取几个数字,不是吗?我找不到你的问题。您面临的问题是什么?

标签: java java.util.scanner sliding-window


【解决方案1】:

不知道您面临什么问题。在伪代码中应该是这样的:

ask for file name;
open inputStream for file;

read 5 characters from inputStream;
phoneNumber = concatenation of 5 character

print phoneNumber;
put phoneNumber in a set;

while (there are more data in inputStream) {
    c = read 1 char from inputStream;
    phoneNumber = substring of phoneNumber from 2nd to end, plus c;
    print phoneNumber;
    if (phoneNumber exists in set) {
          print it is repeated;
    } else {
          add phoneNumber to set;
    }
}

【讨论】:

    【解决方案2】:

    处理“滑动窗口”的一种方法是使用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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-16
      • 2012-05-09
      • 2014-03-28
      • 2015-02-20
      • 2014-07-05
      • 2011-02-25
      • 2017-09-04
      • 2021-08-10
      • 1970-01-01
      相关资源
      最近更新 更多