如何只用一次迴圈輸出完整的九九表?

來源:互聯網
上載者:User
只用一次迴圈輸出如下的內容

1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

回複內容:

只用一次迴圈輸出如下的內容

1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

這算是初級的演算法題麼 ... 很久不寫極限簡單的代碼 ... 我試試看好了 ...

基本原理很簡單 ...

因為只能使用一次迴圈 ... 所以我們需要在 for 迴圈內部控制迴圈的結束 ...

雖然代碼看起來比兩次迴圈要少 ... 但事實上時間複雜度是一樣的 ...

其實吧 ... 產生乘法表的方法有很多種 ... 甚至不需要迴圈都可以解決 ...

使用單次迴圈解決的方法也不僅僅只有我這一種 ... 我能想到的至少還有兩三種 ...

我寫這一個版本 ... 其他的留給別人好啦 ...

沒有實現不了 ... 關鍵點就是在創意恩恩 ...

C語言

void prt_table(){    int i, j;    for (i = 1, j = 1; i < 10 && j < 10;) {        printf("%d*%d=%2d   ", i, j, i * j);        if (i == j) {            puts();            i = 1;            ++j;        }        else if (i < j) {            ++i;        }    }}

puts [*1..9].product([*1..9]).map { |x, y| %Q(#{y} * #{x} = #{x*y}\t#{"\n" if x==y}) if x>=y }.join("")

ruby 1.9.3

站長就不捨得支援下markdown嗎

來個clojure的one-liner:

(dorun (for [o (range 1 10) i (range 1 (+ o 1))] (do (print (str o "x" i "=" (* o i) " ")) (when (= i o) (println)))))

在repl中的輸出:

user=> (dorun (for [o (range 1 10) i (range 1 (+ o 1))] (do (print (str o "x" i "=" (* o i) " ")) (when (= i o) (println)))))1x1=1 2x1=2 2x2=4 3x1=3 3x2=6 3x3=9 4x1=4 4x2=8 4x3=12 4x4=16 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81 nil

雖然我沒看出來在這個很自然的“2層迴圈”的邏輯中非得用“一次迴圈”有什麼好處(省計算?沒有啊?),就當蛋疼吧,來個一次迴圈的:

(loop [o 1 i 1]  (do    (print (str o "x" i "=" (* o i) " "))    (cond (and (= o i) (= o 9)) nil          (= o i) (do                     (println)                    (recur (+ 1 o) 1))          true (recur o (+ 1 i)))))

repl中輸出:

user=> (loop [o 1 i 1]  #_=>   (do  #_=>     (print (str o "x" i "=" (* o i) " "))  #_=>     (cond (and (= o i) (= o 9)) nil  #_=>           (= o i) (do   #_=>                     (println)  #_=>                     (recur (+ 1 o) 1))  #_=>           true (recur o (+ 1 i)))))1x1=1 2x1=2 2x2=4 3x1=3 3x2=6 3x3=9 4x1=4 4x2=8 4x3=12 4x4=16 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81 nil

python中的代碼:

print ('\n'.join([' '.join(['%s*%s=%-2s' % (y,x,x*y) for y in range(1,x+1)]) for x in range(1,10)]))

或者:

for i in range(1,10):    for j in range(1,i+1):        print (str(j),'*',str(i),'=',i*j,end='  ')    print ('\n')input()

(arr = ([*1..9].map{|i| Array.new(i,i).zip([*1..i])}.flatten)).each_index{|i|puts "#{arr[i]}*#{arr[i+1]}=#{arr[i]*arr[i+1]}" if i%2 == 0}

Ruby1.9.3測試通過,一層迴圈,一行代碼,換行這種小事情就不要計較了

有java麼~~我來加個java的~

for (int i = 1, j = 1; i < 10 && j > 0; i++) {  if (j != 10) {    System.out.print(i + "x" + j + "=" + (i * j) + "\t");  }  if (i == j) {  j++;  i = 0;  System.out.println("");  }}1x1=11x2=22x2=41x3=32x3=63x3=91x4=42x4=83x4=124x4=161x5=52x5=103x5=154x5=205x5=251x6=62x6=123x6=184x6=245x6=306x6=361x7=72x7=143x7=214x7=285x7=356x7=427x7=491x8=82x8=163x8=244x8=325x8=406x8=487x8=568x8=641x9=92x9=183x9=274x9=365x9=456x9=547x9=638x9=729x9=81

public static void main(String[] args){
print(1,1);
}

public static void print(int i,int j){
if(i>9 || j>9 || j>i)
return ;
if(i==j){
System.out.println(i+"*"+j+"="+(i*j));
print(i+1,1);
}else{
System.out.print(i+"*"+j+"="+(i*j)+" ");
print(i,j+1);
}

}
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

(->> (range 1 10)           (map            (fn [n] (map #(format "%sx%s=%s" %1 %2 (* %1 %2))                         (range 1 (inc n))                         (repeat n))))           flatten)

Clojure,換行神馬的就不要糾結了。

我是來湊熱鬧的!

void print(int x,int y){printf("%d x %d = %d   ",x,y,x*y);fflush(stdout);if (x == 9 && y == 9) return;if (x == y) {putchar('\n');return print(1,++y);}return print(++x,y);}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.