[python]编程题小代码

100以内奇数的和

sum =0

n =99

while n>0 :

    sum =sum+n

    n =n-2

print(sum )

打字符题

h =int(input("请输入你想打印的行数:"))

n = 1

while n

    print(n*'*')

    n +=1

等腰三角形

layer = int(input("请输入你要打印的行数:"))

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

    spce_num = layer -i

    for j in range(0,spce_num):

        print(" ",end="")

    star_num = 2*i-1

    for j in range(0,star_num):

        print("*",end="")

    print(" ")  

 对称的等腰三角形

layer = int(input("请输入你打印的层数;"))

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

    spce_num =layer -1

    for i in range(0,star_num):

        print(" ",end="")

    star_num = 2*1-1

    for j in range(0,star_num):

        print(" ",end ="")

    print("")    

while layer % 2 == 0 :

    layer = int(input("请输入正确的打印的行数,注意是奇数:"))

for i in range(1,layer// 2+3):

    spce_num = layer-1

    for i in range(0,spce_num):

        print(" ",end="")

    star_num = 2*i-1

    for j in range(0,star_num):

        print("*",end="")

    print("")

for i in range(layer//2,0,-1):

    spce_num =layer -i

    for j in range(0,spce_num):

        print(" ",end="")

    star_num = 2*i-1

    for i in range(0,star_num):

        print("*",end="")

    print("")

计数排序

arr =[7,3,2,0,3,2,1,6]

max_num =arr[0]

min_num =arr[0]

for num in arr:

    if num>max_num:

        max_num =num

    elif num

        min_num =num

offest = min_num

len_arr1 =max_num - min_num +1

arr1 =[0]*len_arr1

arr2 =[0]*len(arr)

for num in arr:

    print(num)

    arr1[num-offest]+=1

print(arr1)

index=0

for i in range(0,len_arr1):

    for j in range(0,arr1[i]):

        arr2[index] = i+offest

        index +=1

print()

print(arr2)

交换变量

num1 = int(input("请输入第一个数:"))

num2 = int(input("请输入第二个数:"))

#方法一:使用中间变量

temp = num1

num1 = num2

num2 = temp

print("交换后,num1 = %s num2 = %s"%(num1,num2))

#方法二:求和法

num1 = num1 + num2

num2 = num1 - num2

num1 = num1 - num2

print("交换后,num1 = %s num2 = %s"%(num1,num2))

#方法三:以后交换法(python独有的)寄存器

num1,num2 = num2,num1

print("交换后,num1 = %s num2 =%s"%(num1,num2))

九九乘法表

a = 1

while a <= 9 :

    b = 1

    while b <= a:

        print("%s*%s=%s"%(a,b,a*b),end="\t")

        b+=1

    print('')

    a+=1

你可能感兴趣的:(python编程小题型,python)