杨辉三角
def yang_hui(x,y):
x=int(x)
y=int(y)
assert x>=y,'列数不应该大于行数'
# x 表示行,y表示列
if y==1 or y==x:
return 1
else:
return yang_hui(x-1,y-1)+yang_hui(x-1,y)
x=input('输入第几行')
y=input('输入第几列')
result=yang_hui(int(x),int(y))
print(result)
#include <iostream>
using namespace std;
int yang_hui(int x, int y)
{
if (y==1)
{
return 1;
}
else
{
if (y == x) {
return 1;
}
else
return yang_hui(x - 1, y - 1) + yang_hui(x - 1, y);
}
}
void main()
{
int x=1, y=1;
int result;
cin >> x >> y;
result = yang_hui(x, y);
cout << "result" << result;
}
二进制
def tenTotwo(number):
s = []
binstring = ''
while number > 0:
rem = number % 2
s.append(rem)
number = number // 2
while len(s) > 0:
binstring = binstring + str(s.pop())
return binstring
number=int(input('输入一个整数'))
bin_num1=bin(number)
print('转换后的二进制',bin_num1)
bin_num2=tenTotwo(number)
print('转换后的二进制',bin_num2)