2016-11-29

1-9位數不重複印出來 (練習問題)

使用者輸入1 印1-9
使用者輸入2 印1-98 (11, 22, 33等重複的不印)
使用者輸入3 印1-987 (121, 988, 667等有重複的不印)


#!/usr/bin/tclsh
#
# Test on Tcl 8.6
#

puts -nonewline "Please input a number: "
flush stdout
gets stdin number
if {$number <= 0 || $number >= 10} {
   puts "The range is 1 - 9."
   exit
}

set max [expr 10 ** $number]

puts "\nNow get the result:"
puts "==========="
puts "Start"
puts "==========="
puts -nonewline "1"
for {set i 2} {$i < $max} {incr i} {
    set var [regexp "1.*1|2.*2|3.*3|4.*4|5.*5|6.*6|7.*7|8.*8|9.*9|0.*0" $i]
    if {$var != 1} {
        puts -nonewline ", $i"
    }
}
puts "\n==========="
puts "End"
puts "==========="

沒有留言: