看板 Perl 關於我們 聯絡資訊
#!/usr/bin/perl sub mysort{#這邊用bubble sort for($i=1;$i<=$#_;$i++){ for($j=0;$j<=$#_-$i;$j++){ ($_[$j],$_[$j+1])=($_[$j+1],$_[$j]) if($_[$j]>$_[$j+1]); } } } print "請輸入你要排序的數字,輸入完成請按ctrl+d結束\n"; chomp (@data = <STDIN>); &mysort(@data); print "@data\n"; ※ 引述《liwei0321 (ㄚ笠伯)》之銘言: : 就是今天我有個排序的題目 可是我不用 sort ($a <=> $b) 下去做 : 我要把我給的幾個數給排序 : 想法是(假如有四個) 兩兩相比 找出第一個最小的 然後刪掉 : 剩下三個在繼續比較.....一直到結束 : 這重複過程就用sub來做 : =================================================== : #!/usr/bin/perl : print "請輸入你要排序的數字,輸入完成請按ctrl+d結束\n"; : chomp (@data = <STDIN>); : open(FILE,">data\n"); : print FILE "@data"; : close(FILE); : my @input = @data; : my @out = sort { $a <=> $b } @input; : open(FILE,">output"); : print FILE "@out\n"; : close(FILE); : 這些是我sort寫出來的 : 希望知道的高手可以教一下 !!!!感謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.63.246.28
liwei0321:感謝!!! 06/11 15:25