自動生成1到100的數字自增序列的幾種方法
==之所以寫這個題目,是原於一道筆試題,加上自己喜歡用的一些小方法,匯總如下:
1.編程語言,此方法由於語言的不同而存在語法上的差異,在此僅簡單舉例如下
python腳本:
多行輸出:
for i in range(1,101) : print i
單行輸出:
' '.join(str(i) for i in range(1,101))
2.PowerShell(windows)
PS腳本如下:
多行輸出:
$num=1..100
$num
單行輸出:
$num=1..100
foreach ($a in $num) {$w=-join($w," ",$a)}
$w
3.批處理BAT(windows)
bat腳本:
多行輸出:
@echo off rem 這里是注釋 for /l %%i in (1,1,100) do echo %%i pause
單行輸出:
@echo off rem 這里是注釋 setlocal EnableDelayedExpansion set str= for /l %%i in (1,1,100) do ( set str=!str! %%i ) echo %str% pause
4.BASH(linux)
腳本:
多行輸出:
for ((i=1;i<101;i++)) do echo $i done
單行輸出:
w="" for ((i=1;i<101;i++)) do w=$w' '$i done echo $w
或者直接
echo {1..100}
命令:
多行輸出:seq 100
單行輸出:seq -s ' ' 100
自動補零:seq -w 1 100
5.數據庫(mysql\oracle)
mysql腳本:
多行輸出:
delimiter // create table t ( id int ) // create procedure proc() begin declare i int default 0; loop_label: loop set i=i+1; insert into t values(i); if i>=100 then leave loop_label; end if; end loop; end; // call proc() // drop procedure proc // select * from t //
單行輸出:
delimiter // drop procedure if exists pro_rep // create procedure pro_rep(num int) begin set @x =1; set @w=''; repeat set @w=concat(@w,' ',@x); set @x = @x + 1; until @x > num end repeat; select @w as x; end // call pro_rep(100) //
oracle腳本:
set serverout on declare num number; begin for num in 1..100 loop dbms_output.put_line(num); end loop; end; /
6.文本編輯器(列模式)
6.1 Excel:(windows)
選中A1,輸入“1”,鼠標移動到該單元格右下角,待箭頭變為+時,向下拖動鼠標到A100單元格,在彈出方塊中選擇“填充序列”
6.2 Vim:(linux)
光標移到某空行開頭,然后按下esc鍵,依次輸入:r !seq 100 然后回車
類似命令有:let i=1 | g/^/s//\=i/ | let i=i+1
6.3 UltraEdit:(windows)
新建文件,按住回車鍵直到湊足100行,ctl+a進行全選,alt+c打開列模式,在菜單欄“列”中選擇“插入號碼”(具體方法因版本不同可能有差異),在彈出的對話框中點擊“確定”,同時可選擇是否“列首補零”
6.4 Sublime Text:
安裝了package control后,在菜單欄“preferences”中選擇“Package Control”,在選擇“Package Control:Install Package”,輸入搜索“Text Pastry”並安裝。
st下列選擇方法為按住鼠標中間向下拖動,待選擇100行后,按ctl+alt+n,在彈出的框中輸入i並回車。如果如下補零則輸入1 1 3即可。