- 求s=a+aa+aaa+aaaa+aa...a的值,其中a是⼀个数字。例如2+22+222+2222+22222(此时共有5个数相加),⼏个数相加有键盘控制。
a = int(input('请输入一个10以内的数字:'))
n = int(input('请输入相加数字的个数:'))
sum1 = 0
sum2 = 0
sum3 = 0
for i in range(0,n):
sum1 += 10**i*a
sum2 = sum1
sum3 +=sum2
print(sum3)
for i in range(5):
print('@'*i)
for i in range(7):
print('@' * i)
for i in range(1,6,2):
str = '*'* i
new_str = str.center(10,' ')
print(new_str)
for i in range(3,0,-1):
print('@' * i)
- 输⼊两个正整数m和n,求其最⼤公约数和最⼩公倍数。
```m = int(input('请输入一个正整数m:'))
n = int(input('请输入一个正整数n:'))
if m > n:
for i in range(n,1,-1):
if m%i == 0 and n%i == 0:
print('最大公约数为%d' % (i))
break
for j in range(m * n, m, -1):
if j % m == 0 and j % n == 0:
x = j
print('最小公倍数为%d' % (x))
else:
for i in range(m,1,-1):
if m % i == 0 and n % i == 0:
print('最大公约数为%d' % (i))
break
for j in range(m * n, n, -1):
if j % m == 0 and j % n == 0:
x = j
print('最小公倍数为%d' % (x))
- ⼀个数如果恰好等于它的因⼦之和,这个数就称为 "完数 "。例如6=1+2+3.编程 找出1000以内的所有完数
for i in range(1,1001):
s = 0
for j in range(1,i):
if i % j == 0:
s += j
if s == i :
print(i)
for i in range(1,10):
for j in range(1,i+1):
print(str(j) ,'*' ,str(i) , '=' , str(i*j),end=' ')
print(' ')
- ⼀个5位数,判断它是不是回⽂数。即12321是回⽂数,个位与万位相同,⼗位与千位相同。
num = int(input('请输入一个五位数:'))
a = num//10000
b = num//1000%10
c = num//10%100
d = num%10
if a == d and b == c:
print('%d是一个回文数'%(num))
print('%d不是一个回文数'%(num))
for i in range(1,8,2):
str = '*'* i
new_str = str.center(10,' ')
print(new_str)
for i in range(5,0,-2):
str1 = '*'* i
new_str1 = str1.center(10,' ')
print(new_str1)
- 输⼊⼀⾏字符,分别统计出其中英⽂字⺟、空格、数字和其它字符的个数。
str = input('请输入一串字符:')
num_alpha = 0
num_space = 0
num_digit = 0
num_other = 0
for i in str:
if i.isalpha():
num_alpha += 1
elif i.isspace():
num_space += 1
elif i.isdigit():
num_digit += 1
else:
num_other += 1
print('字母个数为%d,空格个数为%d,数字个数为%d,其他字符个数为%d' % (num_alpha,num_space,num_digit,num_other))