1、~12为什么是-13?
解1:12 二进制:0000 1100
补码: 0000 1100
按位取反 :1111 0011
求这个原码,即负数求补码的逆过程:先减1,再取反 (符号位不变)
-1 1111 0010
取反 1111 1101 还原成十进制-13
2、10^9等于?10^-9等于?
解:(1)10^9
10的二进制:0000 1010
9的二进制: 0000 1001
异或(同为1,异为0)结果:
0000 0011 为十进制3
(2)10^-9
10的二进制补码:0000 1010
-9的二进制为其补码:
1000 1001
取反 1000 0110
加1 1000 0111 此即补码
异或:0000 1001
1000 0111
结果:1000 1110 然后求其原码
减1 :1000 1101
取反: 1000 0010 即十进制-2
3、输入两个数字,输出最大数
解答: a = int(input(‘请输入数字1:’))
b = int(input(‘请输入数字2:’))
print(a,b)
if a>=b:
print(a)
else:
print(b)
4、给定一个不超过5位的正整数,判断其有几位
(使用input函数)
解:
a = int(input(‘请输入不超过五位数的正整数:’))
print(a)
if a > 999:
if a > 9999:
print(‘位数是5’)
else:
print(‘位数是4’)
elif a > 9:
if a > 99:
print(‘位数是3’)
else:
print(‘位数是2’)
else:
print(‘位数是1’)
5、计算10以内的偶数(for循环)
解答:
(1) for i in range(0,10,2):
print(i)
(2) for i in range(10):
if i & 0b1:
continue
print(i)
6、计算1000以内的被7整除的前20个数(for循环)
解答:
count = 0
for i in range(0,1000,7):
print(i)
count += 1
if count >= 20:
break
7、给定一个不超过5位的正整数,判断该数的位数,依次打印出个位、十位、百位、千位、万位的数字。或者,依次打印出万位到个位的数字。
解答:
val = int(input(‘Please input a int > 0 & < 100000:>>>’))
print(val)
#查看位数
if val >= 1000:
if val >= 10000:
num = 5
else:
num = 4
else:
if val >= 100:
num = 3
elif val >= 10:
num = 2
else:
num = 1
print(num)
#打印个位、十位、百位、千位、万位
a = 1
while a:
a = val % 10
print(a)
val //= 10
if val < 1:
break
# 另一种方法
for i in range(num):
n = val // 10
print(val – n*10)
val = n
#倒序打印
for j in range(num,0,-1):
b = val // 10**(j-1)
print(b)
val = val – b*10**(j-1)
#另一种方法
pre = 0
for j in range(num,0,-1):
c = val // 10**(j-1)
print(c – pre*10)
pre = c
8、打印一个边长为n的正方形
解答:
n = int(input(‘Please input the length of square: ‘))
for i in range(1,n+1):
if i == 1 or i == n:
print(‘*’*n)
else:
print(‘*’+’ ‘*(n-2)+’*’)
9、求100内所有奇数的和
解答:
sum = 0
for i in range(1,100,2):
sum += i
print(sum)
10、判断学生成绩,成绩等级A~E。其中,90分以上为A,80~89分为B,70~79为C,60~69为D,60分以下为E。
解答:
score = float(input(‘Please input the score: ‘))
if score >= 80:
if score >= 90:
level = ‘A’
else:
level = ‘B’
elif score >= 70:
level = ‘C’
elif score >= 60:
level = ‘D’
else:
level = ‘E’
print(level)
11、求1到5阶乘之和
解答:
n = int(input())
sum = 0
for i in range(1,n+1):
a = 1
for j in range(1,i+1):
a *= j
sum += a
print(sum)
12、给一个数,判断它是否是素数(质数:一个大于1的自然数只能被1和它本身整除)
解答:
#方法1,数n本身与2到(n-1)整除
num = int(input(‘请输入大于1的数’))
i = int()
for i in range(2,num):
if num%i == 0:
print(‘这不是一个素数’)
break
else:
print(‘这是一个素数’)
#方法2,优化范围(1,sqrt(n)),平方根两边只用检查一遍
flag = False
for i in range(2,int(n**0.5)+1):
if n % i == 0:
print(n,’is not a prime number’)
break
else:
print(n,’is a prime number’)
#方法3,
13、打印九九乘法表
解答:
for i in range(1,10):
for j in range(1,i+1):
product = i*j
if j > 1 and j * i < 10:
product = str(product) + ‘ ‘
else:
product = str(product)
print( ‘{}*{}={}’.format(j,i,product),end=’ ‘)
print()
14、打印下图菱形
解答: 下面代码输入7即可
n = int(input())
e = n // 2
for i in range(-e,e+1):
print(‘ ‘*abs(i)+”*”*(n-2*abs(i)))
15、打印100以内的斐波那契数列
解答:
print(0)
print(1)
a = 0
b = 1
while True:
c = a + b
if c > 100: break
a = b
b = c
print(c)
16、求斐波那契数列第101项
解答:
a = 0
b = 1
for i in range(2,102):
c = a + b
a = b
b = c
print(b)
17、求10万内的所有素数
解答:
方法1:
count = 1
for x in range(3,100000,2):
if x > 10 and x // 10 == 5:
continue
for i in range(3,int(x**0.5)+1,2):
if x % i == 0:
break
else:
count +=1
print(x)
方法2:考虑下计数,引入时间
import datetime
start = datetime.datetime.now()
count = 0
sum = 0
for i in range(3,100000,2):
for j in range(3,int(i**0.5+1),2):
sum += 1
if not i % j:
break
else:
count += 1
print(count)
print(sum)
delta = (datetime.datetime.now()-start).total_seconds()
print(delta)
18、给一个半径,求圆的面积和周长。圆周率3.14
解答:
r = int(input(‘r=’))
print(‘area=’+str(3.14*r*r))
print(‘circumference=’+str(2*3.14*r))
19、输入两个数字,比较大小后,从小到大升序打印
解答:
a = input(‘first: ‘)
b = input(‘second: ‘)
if a > b:
print(b,a)
else:
print(a,b)
#采用三元表达式
a = input(‘first: ‘)
b = input(‘second: ‘)
print(b,a) if a > b else print(a,b)
20、获取最大值,请输入若干个整数,打印出最大值
解答:
#以空输入结束循环
n = int(input(‘Input first number:’))
while True:
m = input(‘Input second number:’)
if m:
m = int(m)
if m > n:
n = m
print(‘Max is’, n)
else:
break
21、输入若干个数,求每次输入后的算数平均数
解答:
n = 0
sum = 0
while True:
a = input(‘>>’)
if a == ‘quit’:
break
n +=1
sum += int(a)
print(‘arithmetic average is ‘,sum / n)
22、解决猴子吃桃问题
若干个桃子,第一天吃掉一半零一个,每天如此,第10天只剩1个,问总共有多少桃子?
解答:
n = 1
for i in range(2,11):
b = 2 * (n + 1)
n = b
print(n)
print(‘———————-‘)
n = 1534
for i in range(2,11):
b = n/2 – 1
n = b
print(int(n))
23、使用右对齐打印九九乘法表
解答:
for i in range(1,10):
s = ”
for j in range(i,10):
s += ‘{}*{}={:<{}} ‘.format(i,j,i*j,2 if j < 4 else 3)
print(‘{:>70}’.format(s))
24、打印对顶三角形
解答:
n = int(input(‘Input the number:’))
e = n // 2
for i in range(-e,e+1):
print(‘ ‘*(e-abs(i))+’*’*(2*abs(i)+1))
25、打印闪电
解答:
n = 7
for i in range(-3,4):
if i < 0:
print(“{:>4}”.format(‘*’*(4+i)))
elif i == 0:
print(‘*’*7)
else:
print(” ” * 3 + “*” *(4-i))
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/92858