112年關務人員四等程式設計概要

三、下列是以 Python 程式語言撰寫的片段程式,試回答每一小題的輸出結果。每一小題是獨立運作的。注意,若迴圈無法停止,則以無窮迴圈作答。(每小題3分,共30分)

    () total = 0

        for i in range(1, 100):

            total += i

        print('total = ', total)

    () total = 0

        i = 1

        while i <= 100:

            total += i

            i += 1

        print('total = ', total)

    () total = 0

        i = 1

        while i <= 100:

            i += 1

            total += i

        print('total = ', total)

    () total = 0

        for i in range(2, 100, 2):

            total += i

        print('total = ', total)

    () total = 0

        i = 1

        while i <=100:

            total += 1

            i += 1

        print('total = ', total)

    () total = 0

        i = 1

        while i <= 100:

            total += i

        i += 2

        print('total = ', total)

    () total = 0

        for i in range(100, 1, -1):

            total += i

        print('total = ', total)

    () total = 0

        i = 100

        while i > 0:

            total += i

            i -= 1

        print('total = ', total)

    ()下列的程式,若依序輸入的數值是123456

        total = 0

        i = 1

        while i <= 5:

            num = eval(input('Enter a number: '))

            if num % 5 == 0:

                break

            else:

                total += num

            i += 1

        print('total = ', total)

    ()下列的程式,若依序輸入的數值是123456

        total = 0

        i = 1

        while i <= 5:

            num = eval(input('Enter a number: '))

            if num % 5 == 0:

                continue

            else:

                total += num

            i += 1

        print('total = ', total)

答:

()

total = 0

for i in range(1, 100):

    total += i

print('total = ', total)

執行結果:

total = 4950

說明:

i

1

2

3

99

total

1

3

6

4950

total = 1+2+3+…+99 =01.png=02.png= 99×50 = 4,950

()

total = 0

i = 1

while i <= 100:

    total += i

    i += 1

print('total = ', total)

執行結果:

total = 5050

說明:

i

1

2

3

100

total

1

3

6

5050

total = 1+2+3+…+100 =03.png=04.png= 50×101 = 5,050

()

total = 0

i = 1

while i <= 100:

    i += 1

    total += i

print('total = ', total)

執行結果:

total = 5150

說明:

i

1

2

3

100

total

2

4

7

5150

total = 2+3+…+101=05.png=06.png= 50×103 = 5,150

()

total = 0

for i in range(2, 100, 2):

    total += i

print('total = ', total)

執行結果:

total = 2450

說明:

i

2

4

6

98

total

2

6

12

2450

total = 2+4+…+98 =07.png=08.png= 49×50 = 2,450

()

total = 0

i = 1

while i <=100:

    total += 1

    i += 1

print('total = ', total)

執行結果:

total = 100

說明:

i

1

2

3

100

total

1

2

3

100

()

total = 0

i = 1

while i <= 100:

    total += i

i += 2

print('total = ', total)

執行結果:

無窮迴圈

()

total = 0

for i in range(100, 1, -1):

    total += i

print('total = ', total)

執行結果:

total = 5049

說明:

i

100

99

98

2

total

100

199

297

5049

total = 100+99+…+2 =09.png=10.png= 99×51 = 5,049

()

total = 0

i = 100

while i > 0:

    total += i

    i -= 1

print('total = ', total)

執行結果:

total = 5050

說明:

i

100

99

98

1

total

100

199

297

5050

total = 100+99+98+…+1 =11.png=12.png= 50×101 = 5,050

()

下列的程式,若依序輸入的數值是123456

total = 0

i = 1

while i <= 5:

    num = eval(input('Enter a number: '))

    if num % 5 == 0:

        break

    else:

        total += num

    i += 1

print('total = ', total)

執行結果:

Enter a number: 1

Enter a number: 2

Enter a number: 3

Enter a number: 4

Enter a number: 5

total = 10

說明:

i

1

2

3

4

total

1

3

6

10

()

下列的程式,若依序輸入的數值是123456

total = 0

i = 1

while i <= 5:

    num = eval(input('Enter a number: '))

    if num % 5 == 0:

        continue

    else:

        total += num

    i += 1

print('total = ', total)

執行結果:

Enter a number: 1

Enter a number: 2

Enter a number: 3

Enter a number: 4

Enter a number: 5

Enter a number: 6

total = 16

說明:

i

1

2

3

4

6

total

1

3

6

10

16

 

arrow
arrow
    文章標籤
    關務人員四等程式設計概要
    全站熱搜
    創作者介紹
    創作者 jacksaleok 的頭像
    jacksaleok

    國考資訊處理工作室(高考二級資訊處理/高考三級資訊處理/調查局三等/關務人員三等/地方特考三等)

    jacksaleok 發表在 痞客邦 留言(0) 人氣()