-1

次のような宿題があります。

「負でない整数のストリームを、1 番目に 3 で割り切れる整数、2 番目に 1 mod 3 に相当する整数、最後に 2 mod 3 に相当する整数で構成されるストリームに分離するプログラムを作成します。あなたはプログラムします。 0 から 100 までの 20 のランダムな整数を生成することによって着信ストリームをシミュレートし、最大 4 つの整数スタックを使用して問題を解決する必要があります。」

私はストリームにあまり詳しくありません。ストリームに関する私の経験は、ファイルからの単純な読み取りと書き込みに限定されていました。当然のことながら、ソリューションについて考えていたとき、ファイルを生成して 20 個のランダムな整数を書き込むことができると考えていました。次に、それを読み込んで、着信ストリームをシミュレートします。

ここで混乱し始めます。なぜなら、ストリームをストリーム (???) に分離することになっているからです。x%3=0、x%3=1、x%3=2 という分離の数学的な側面は理解していますが、ここで新しいストリームを作成する必要がありますか? では、入力ストリームを取得して、それを出力ストリームに「分離」しますか?

次に、彼は最大 4 つのスタックを使用することについて言及していますが、整数を新しいストリームに入れる前にスタックを使用して整数を保持する必要がない限り、この問題を解決するためにスタックを使用する必要がある場所がわかりません。

この問題に対する私の理解が論理的で正しいかどうか教えてください。

編集:私の現在の解決策

import java.io.*;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;

/**
 * write a program which will seperate a stream of nonnegative integers into a stream     consisting of, firstly , the
 * integers which are divisible by 3, secondly, the integers equivalent to 1 modula three, and lastly, the integers
 * equivalent to 2 modulo 3.  Your program should simulate the incoming stream by generating 20 random integers between
 * 0 and 100 and should use at most 4 stack of integers to solve the problem.
*/
public class StreamSeperator {
public static void main (String[] args)
{
    /*
     *  Generate file to simulate incoming stream
     */
    int arr[] = new int[20];
    Random rand = new Random();
    for(int i = 0; i<20;i++){
        arr[i] = rand.nextInt(100);
    }
    try{
        File inputFile = new File("stream_sim.txt");

        if(!inputFile.exists()){
            inputFile.createNewFile();
        }
        FileWriter inputWriter = new FileWriter(inputFile.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(inputWriter);
        for(int j=0;j<arr.length-1;j++){
            bw.write(String.valueOf(arr[j]));
            bw.newLine();
        }
        bw.close();
        System.out.println("Sim file generated");
    }catch (IOException e){
        e.printStackTrace();
    }
    /*
     * Read file in and sort into stacks
     */
    Stack<Integer> divisByThree = new Stack<Integer>();
    Stack<Integer> remainderOne = new Stack<Integer>();
    Stack<Integer> remainderTwo = new Stack<Integer>();

    BufferedReader br = null;
    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader("stream_sim.txt"));

        while ((sCurrentLine = br.readLine()) != null) {
            int _temp = Integer.parseInt(sCurrentLine);
            //divisible by three
            if(_temp%3==0){
                divisByThree.push(_temp);
            }
            //remainder of one
            else if(_temp%3==1){
                remainderOne.push(_temp);
            }
            //remainder of two
            else if(_temp%3==2){
                remainderTwo.push(_temp);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    /*
     * Feed stacks into output stream
     */
    try{
        File outFile = new File("out.txt");
        if(!outFile.exists()){
            outFile.createNewFile();
        }
        FileWriter outWriter = new FileWriter(outFile.getAbsoluteFile());
        BufferedWriter outbw = new BufferedWriter(outWriter);
        Iterator itStack1 = divisByThree.iterator();
        Iterator itStack2 = remainderOne.iterator();
        Iterator itStack3 = remainderTwo.iterator();
        //first stack
        while(itStack1.hasNext()){
            outbw.write(String.valueOf(divisByThree.pop()));
            outbw.newLine();
        }
        //second stack
        while(itStack2.hasNext()){
            outbw.write(String.valueOf(remainderOne.pop()));
            outbw.newLine();
        }
        //thrid stack
        while(itStack3.hasNext()){
            outbw.write(String.valueOf(remainderTwo.pop()));
            outbw.newLine();
        }
        outbw.close();
        System.out.println("Out file generated");
    }catch (IOException e){
        e.printStackTrace();
    }
}

}

4

1 に答える 1