Created
May 22, 2011 07:56
-
-
Save nappa7878/985266 to your computer and use it in GitHub Desktop.
配列をランダムに並び替える
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type | |
//配列の個数を決めないで宣言します | |
TArrayOfInteger = array of integer; | |
//途中略 | |
procedure TForm1.Button1Click(Sender: TObject); | |
var | |
i:integer; | |
rp:TArrayOfInteger; | |
num: integer; | |
myFluit: array[0..5] of string; | |
begin | |
myFluit[0] := 'リンゴ'; | |
myFluit[1] := 'ミカン'; | |
myFluit[2] := 'バナナ'; | |
myFluit[3] := 'ブドウ'; | |
myFluit[4] := 'イチゴ'; | |
num := 5; | |
randPerm(num,ro); | |
for i := 0 to num-1 do | |
//myFluitをランダムに表示する | |
showmessage(myFluit[rp[i]]); | |
end; | |
//配列の参照渡しをするので、var を忘れないようにします | |
procedure TForm1.randPerm(tN: integer;var ro: TArrayOfInteger); | |
var | |
rn: array of integer; | |
i,j: integer; | |
temp,temp2: integer; | |
begin | |
//配列の数を決めます | |
setLength(ro,tN); | |
setLength(rn,tN); | |
for i := 0 to tN-1 do | |
begin | |
ro[i] := i; | |
rn[i] := random(10000); | |
end; | |
for i:=0 to tN-1 do | |
begin | |
for j:=tN-1 downto i do | |
begin | |
if rn[j]<rn[j-1] then | |
begin | |
temp := rn[j]; | |
temp2 := ro[j]; | |
rn[j] := rn[j-1]; | |
ro[j] := ro[j-1]; | |
rn[j-1] := temp; | |
ro[j-1] := temp2; | |
end; | |
end; | |
end; | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment