print input
1、一支铅笔一元二角,10元钱可以买几支铅笔?剩余多少钱。
print(100//12)
print(100-100//12*12)
2、输入一个5位数整数,分别输出个位、十位、百位、千位和万位。
a = int(input())
print(a%10)
print(a%100//10)
print(a%1000//100)
print(a%10000//1000)
print(a//10000)
3、输入一个三位数,然后反向输出。例如:输入268,输出862。
b = int(input())
print(b%10*100+b%100//10*10+b%1000//100)
4、有一个“就是它”的数字游戏。步骤如下:任意输入一个三位数,将这个三位数重复一遍,368→368368 ,得到一个六位数,把六位数连续除以7、11、13,最后得到的商就是原来的三位数。
c = int(input())
print((c*1000+c)/7/11/13)
5、鸡兔同笼,共有35个头,94条脚,求鸡和兔子各有多少只。
print(35-(94/2-35))
print(94/2-35)
if语句
1、输入一个整数,判断是奇数还是偶数。
a = int(input())
if a%2==1:
print("奇数")
else:
print("偶数")
2、输入一个小于100000的整数,输出该整数是几位数。
b = int(input())
if b<10:
print(1)
elif b<100:
print(2)
elif b<1000:
print(3)
elif b<10000:
print(4)
elif b<100000:
print(5)
3、输入年份和月份,输出该月份有多少天。
y = int(input("input year:"))
m = int(input("input month:"))
if m==1 or m==3 or m==5 or m==7 or m==8 or m==10 or m==12:
print(31)
elif m==4 or m==6 or m==9 or m==11:
print(30)
elif m==2 and (y%4==0 and (y%100!=0 or y%400==0)):
print(28)
elif m==2:
print(29)
4、输入三个整数,按由小到大顺序输出。
n1 = int(input())
n2 = int(input())
n3 = int(input())
if n1<=n2:
if n1<=n3:
print(n1)
if n2<=n3:
print(n2)
print(n3)
else:
print(n3)
print(n2)
elif n1>n3:
print(n3)
print(n1)
print(n2)
elif n2<=n3:
print(n2)
if n1<=n3:
print(n1)
print(n3)
else:
print(n3)
print(n1)
else:
print(n3)
print(n2)
print(n1)
5、已知鸡和兔子的总数量是n,总腿数为m。输入n和m,输出鸡和兔子的数目。无解输出 no answer。
n = int(input())
m = int(input())
if m/2-n<=n and m/2-n>=0 and m%2==0:
print(n-(m/2-n))
print(m/2-n)
else:
print("no answer")
while语句
1、输入一个整数,反向输出,例如 12345 -> 54321。
a = int(input())
n = a
i = 0
s = 0
while n!=0:
n //= 10
i += 1
j = i
while i > 0:
s += a%(10**i)//(10**(i-1))*(10**(j-i))
i -= 1
print(s)
2、计算1到10000,每个阿拉伯数字符号出现的次数。(输入一个个位数,输出该数出现的次数)
n = int(input())
i = 1
s = 0
while i<=10000:
m = i
l = 0
while m!=0:
m //= 10
l += 1
m = i
j = 1
c = 0
while j<=l:
c = m%(10**j)//(10**(j-1))
if c==n:
s += 1
j += 1
i += 1
print(s)
3、猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘多少个桃子?
d = 9
s = 1
while d>=1:
s = (s+1)*2
d -= 1
print(s)
4、编程输出一个菱形。
n = 5
j = 1
while j <= n:
k = 1
while k <= n-j:
print(" ",end="")
k += 1
i =1
while i <=2*j-1:
print("*",end="")
i += 1
print()
j += 1
j = n-1
while j >= 1:
k = 1
while k <= n - j:
print(" ", end="")
k += 1
i = 1
while i <= 2 * j - 1:
print("*", end="")
i += 1
print()
j -= 1
5、编程输出斐波那契数列前100项。
a1 = 1
a2 = 1
t = 1
i = 1
while i<=100:
print(a1)
t = a2
a2 = a1+a2
a1 = t
i += 1
字符串
- 统计字符串中单词的个数。
s = " asdsad asfdsa sgsdg sfs "
old_char = " "
i = 0
n=0
while i<len(s):
if (old_char == " " and s[i]!=" "):
n += 1
old_char = s[i]
i += 1
print(n)
- 去除字符串中重复的字符(只保留一个)。例如:abbbccdeffff
结果为:abcdef
s = "abbcccdddeef"
s2 = ""
i = 0
old_char = ""
while i<len(s):
if not (old_char == s[i]):
s2 += s[i]
old_char = s[i]
i += 1
print(s2)
- 输入一个只包含一个运算符的算术表达式,计算其结果。
例如:12+36
s = input("input expression:")
if "+"in s:
add_pos = s.find("+")
left = s[:add_pos]
right = s[add_pos+1:]
print(int(left)+int(right))
if "-"in s:
pos = s.find("-")
left = s[:pos]
right = s[pos+1:]
print(int(left)-int(right))
if "*"in s:
pos = s.find("*")
left = s[:pos]
right = s[pos+1:]
print(int(left)*int(right))
if "/"in s:
pos = s.find("/")
left = s[:pos]
right = s[pos+1:]
print(int(left)/int(right))
for loops
- 统计字符串中每个单词出现的次数。
s = 'asdasfasfsasfz'
a = [0]*26
i = 0
while i<len(s):
a[ord(s[i])-ord('a')] += 1
i += 1
print(a)
for x in range(11,1,-2):
print(x)
for i,x in enumerate(['a','b','c','d','e']):
print(i,x)
- 求表达式中不包含在括号内的最右侧的加号或者减号的位置,没有返回-1. 例如: 12+5*(2+3)+9 返回 10 (9左侧的加号) .
s = '12+5*(2+3)+9'
f = 0
for x in range(len(s)-1,-1,-1):
if s[x]==')':
f -= 1
if s[x]=='(':
f += 1
if f ==0 and (s[x]=='+' or s[x]=='-'):
print(x)
break