1.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
n = input('请输入n:')
sum1 = 0
sum2 = 0
for indx in range(int(n)):
sum1 += 2*10**indx
sum2 += sum1
print(sum1)
print(sum2)
2.打印图形1
n = 5
print('n = ',n)
for indx in range(1,n+1):
print_1 = ('@'*indx)
print(print_1)
3.打印图形2
n = 5
print('n = ',n)
for indx in range(1,n+1):
print(' '*(n-indx),'@'*(2*indx-1),' '*(n-indx))
4.打印图形3
n = 3
print('n = ',n)
for indx in range(n,0,-1):
print('@'*indx)
5.输入两个正整数m和n,求其最大公约数和最小公倍数
m = int(input('请输入正整数m:'))
n = int(input('请输入正整数n:'))
x = m*n
while x >= 0:
if m%x == 0:
if n%x == 0:
print('m,n的最大公约数:', x)
break
x -= 1
while x >= 0:
if x%m == 0:
if x%n == 0:
print('m,n的最小公倍数:', x)
break
x += 1
6.一个数如果恰好等于它的因子之和,这个数就称为“完数“。
例如:6=1+2+3,找出1000内所有完数
x = 1
while x <= 1000:
sum1 = 0
for y in range(1,x):
if x%y == 0:
sum1 += y
if x == sum1:
print(x)
x+=1
结果
6
28
496
7.输出99乘法表
for i in range(1, 10):
for j in range(1, i+1):
print('%d*%d=%d\t'%(i, j, i*j), end='')
print()
8.一个5位数,判断它是不不是回文数。即12321是回文数,个位与万位相同,十位与千位相同
number = int(input('请输入一个五位整数:'))
a = number//10000
b = number//1000%10
c = number//100%100%10
d = number//10%1000%100%10
e = number%10
if a == e and b == d:
print('%d是回文数'%(number))
else:
print('%d不是回文数'%(number))
9.打印菱形
n = 4
for x in range(1,n*2):
if x<=n:
for num1 in range(n-x):
print(' ', end='')
for num2 in range(x*2-1):
print('@', end='')
else:
for new_num1 in range(x - n):
print(' ', end='')
for new_num2 in range((2*n-x)*2-1):
print('@', end='')
print(' ')
10.输入行行字符,分别统计出其中英文,字母、空格、数字和其它字符的个数。
str = input('请输入一串字符:')
count1 = 0
count2 = 0
count3 = 0
count4 = 0
for i in str:
if i.isalpha():
count1 += 1
elif i.isspace():
count2 += 1
elif i.isdigit():
count3 += 1
else:
count4 += 1
print('字母个数为%d,空格个数为%d,数字个数为%d,其他字符个数为%d' % (count1,count2,count3,count4))