雙層迴圈經典小專案題

2022-06-30 13:33:10 字數 3781 閱讀 3737

# (1) 99 乘法表 方向一

i = 1

while i<=9:

# 列印表示式

j = 1

while j <= i:

print("%d*%d=%2d " % (i,j,i*j) ,end="")

j+=1

# 列印換行

print()

i+=1

# 方向二

i = 1

while i<=9:

# 列印空格

k = 9 - i

while k>0:

print(" ",end="")

k -= 1

# 列印乘法表示式

j = 1

while j<=i:

print("%d*%d=%2d " % (i,j,i*j),end="")

j+=1

# 列印換行

print()

i+=1

"""k = 8

while k>0:

print(" " * k,end="")

print(1)

k-=1

1 => 8

2 => 7

3 => 6

4 => 5

...8 => 1

"""# 方向三

print("<==>")

i = 9

while i>0:

# 列印表示式

j = 1

while j <= i:

print("%d*%d=%2d " % (i,j,i*j) ,end="")

j+=1

# 列印換行

print()

i-=1

# 方向四

i = 9

while i>0:

# 列印空格

k = 9 - i

while k>0:

print(" ",end="")

k -= 1

# 列印乘法表示式

j = 1

while j<=i:

print("%d*%d=%2d " % (i,j,i*j),end="")

j+=1

# 列印換行

print()

i-=1

# 1.100~ 999 找吉利數字 111 222 333 ... 999 123 321 456 654 ..678 876 列印出來

# 寫法一

'''789

個位: 789 % 10

十位: 789 // 10 % 10 => 78 % 10 => 8

百位: 789 // 100

'''i = 100

while i<=999:

# 算出個位

gewei = i % 10

# 算出十位

shiwei = i // 10 % 10

# 算出百位

baiwei = i // 100

if shiwei == gewei and shiwei == baiwei :

print(i)

# 123 678

if shiwei == gewei-1 and shiwei == baiwei + 1:

print(i)

# 321 876

if shiwei == gewei+1 and shiwei == baiwei - 1:

print(i)

i+=1

# 寫法二

'''strvar = "789"

strvar[0] => "7"

strvar[1] => "8"

strvar[-1] => "9"

'''print("<*************************=>")

'''i = 100

while i<=999:

strvar = str(i)

# 算出個位

gewei = int(strvar[0])

# 算出十位

shiwei = int(strvar[1])

# 算出百位

baiwei = int(strvar[-1])

if shiwei == gewei and shiwei == baiwei :

print(i)

# 123 678

if shiwei == gewei-1 and shiwei == baiwei + 1:

print(i)

# 321 876

if shiwei == gewei+1 and shiwei == baiwei - 1:

print(i)

i+=1

'''# 2.百錢買百雞 公雞1塊錢乙隻 母雞 3塊錢乙隻 小雞5毛錢乙隻 100塊錢 買 100隻雞 有多少種買法(公升級)

"""# 窮舉法: 乙個乙個的試

公雞x 母雞y 小雞z x+y+z = 100

x 1,2

y 3,4

z 5,6

x+y+z=10 ?

x = 1

1 + 3 + 5

1 + 3 + 6 = 10

1 + 4 + 5 = 10

1 + 4 + 6

x = 2

2 + 3 + 5 = 10

2 + 3 + 6

2 + 4 + 5

2 + 4 + 6

"""x = 0

while x <= 100:

y = 0

while y<=33:

z = 0

while z<=100:

if (x+y+z == 100) and (x*1 + 3*y + 0.5 * z == 100):

print(x,y,z)

z+=1

y+=1

x+=1

# 7.寫**:計算 1 - 2 + 3 -4 + 5 -6 ... ... + 99 中除了88以外所有數的總和?

i = 1

total = 0

while i <= 99:

if i == 88:

i+=1

continue

if i % 2 == 1:

# 1 + 3 + 5+ 7 + 9...

total += i

# total = 1 + 3 + 5 +7 ...

else:

# -2 -4 -6 -8 -10.....

total -= i

# total = -2 -4 -6 -8

i+=1

print("<===>")

print(total)

# 2500 - 2450 = 50

# 2450

# for迴圈自動完成加一操作

total = 0

for i in range(1,100):

if i == 88:

continue

if i % 2 == 1:

total += i

else:

total -= i

print(total)

"""9~1

for i in range(9,0,-1)

print(i)

j = 1

while j<=i:

pass

for i in range(1,i+1):

pass # 1 ~ i

"""

經典python基礎小專案練習

3.公升級題 實現乙個整數加法計算器 多個數相加 如 content input 請輸入內容 使用者輸入 5 9 6 12 13,然後進行分割再進行計算。content input 請輸入內容 print content listvar content.split print listvar res...

for雙層迴圈詳解

include main x printf x d n x printf x d n x 123456788 int i,j,m 0,n 0 for i 0 i 2 i printf d n n 1 includemain system.out.println 首先程式會執行 給i初始化賦值0 然後...

Java跳出雙層for迴圈

例1 跳出單層迴圈 test public void test1 else 執行結果 i 0 i 1例2 跳過單層當次迴圈 test public void test2 else 執行結果 i 0 i 1i 3 i 4 例3 跳出雙層的內部當次迴圈 test public void test3 el...