1

0 から 255 までのn桁を含む数字のみ(スペース、コンマ、改行などを含まない)のプレーンテキスト ファイルがあります。それを取り込んで、これらの値を配列に格納したいと考えています。


ファイルに次のシーケンスがあるとします。

581060100962552569

私はこのようにそれを取り入れたい.in.readはファイル入力ストリームでtempArrayあるendArray.

in.read     tempArray     endArray
5           [5][ ][ ]     [] //It reads in "5", sees single-digit number X guarantees that "5X" is less than or equal to 255, and continues
8           [5][8][ ]     [58] //It reads in "8", realizes that there's no number X that could make "58X" smaller than or equal to "255", so it stores "58" in endArray
1           [1][ ][ ]     [58] //It wipes tempArray and reads the next value into it, repeating the logic of the first step
0           [1][0][ ]     [58] //It realizes that all single-digit numbers X guarantee that "10X" is less than or equal to "255", so it continues
6           [1][0][6]     [58][106] //It reads "6" and adds "106" to the endArray
0           [0][ ][ ]     [58][106] //It wipes tempArray and stores the next value in it
1           [0][1][ ]     [58][106]
0           [0][1][0]     [58][106][10] //Even though all single-digit numbers X guarantee that "010X" is less than or equal to "255", tempArray is full, so it stores its contents in endArray as "10".
0           [0][ ][ ]     [58][106][10]
9           [0][9][ ]     [58][106][10]
6           [0][9][6]     [58][106][10][96] //Not only can "96" not have another number appended to it, but tempArray is full
2           [2][ ][ ]     [58][106][10][96]
5           [2][5][ ]     [58][106][10][96] //There are numbers that can be appended to "25" to make a number less than or equal to "255", so continue
5           [2][5][5]     [58][106][10][96][255] //"5" can be appended to "25" and still be less than or equal to "255", so it stores it in tempArray, finds tempArray is full, so it stores tempArray's values in endArray as "255"
2           [2][ ][ ]     [58][106][10][96][255][37]
5           [2][5][ ]     [58][106][10][96][255][37] //There are numbers that can be appended to "25" to make a number less than or equal to "255", so continue
6           [6][ ][ ]     [58][106][10][96][255][37][25] //It sees that adding "6" to "25" would make a number that's larger than 255, so it stores "25" in the endArray and remembers "6" in the tempArray
9           [6][9][ ]     [58][106][10][96][255][37][25][69] //It sees that there is no number X such that "69X" is less than "255", so it stores "69" in endArray

この動作を達成する方法を知っている人はいますか? 多くのプログラミング言語に翻訳できるように、回答を疑似コードに保つようにしてください。

4

2 に答える 2

4

中間数値を保持するために一時配列を使用しません。CPU 数値はバイナリ形式で格納されており、10 進数を読み取っているためです。

このようなものはあなたの問題を解決することができます:

array = []
accumulator = 0
count = 0
while not EOF:
    n = readDigit()
    if accumulator*10 + n > 256 or count == 2:
         array.push(accumulator)
         accumulator = n
         count = 0
    else:
         accumulator = accumulator*10 + n
         count = count + 1

結果は、 という配列に追加されarrayます。

編集:不足しているカウンターに気づいてくれた DeanOC に感謝します。しかし、DeanOC のソリューションは、最初の反復のカウンターを 1 ではなく 0 に初期化します。

于 2012-10-20T19:03:58.173 に答える
2

アンチグルの反応はもうすぐです。

主な問題は、数字が 3 桁しかないことを考慮していないことです。この変更はうまくいくはずです。

array = []
accumulator = 0
digitCounter = 0

while not EOF
    n = readDigit()

    if accumulator*10 + n > 255 or digitcounter = 3:
         array.push(accumulator)
         accumulator = n
         digitCounter = 1
    else:
         accumulator = accumulator*10 + n
         digitCounter = DigitCounter + 1
于 2012-10-20T19:30:14.770 に答える