def digui(n):
ans = 0
if n <= 2:
ans = n
else:
ans = digui(n - 1) + digui(n - 2)
return ans
while True:
n = int(input())
print(digui(n))
思路:青蛙跳台阶问题
def digui(n):
ans = 0
if n <= 2:
ans = n
else:
ans = digui(n - 1) + digui(n - 2)
return ans
while True:
a, b = map(int, input().split())
print(digui(b-a))
思路:和上一题一样
def digui(n):
if n == 0:
return 0
else:
return (n ** 2 - n) ** 2 + digui(n - 1)
n = int(input())
print(digui(n))
注意边界条件
def digui(n):
if n == 1:
return 1 / 2
else:
if (n - 1) % 2 == 0:
return 1 / (n ** 2 + n) + digui(n - 1)
else:
return -1 / (n ** 2 + n) + digui(n - 1)
print("%.6f" % digui(int(input())))
注意处理正负号
def digui(n):
if n == 1:
return 2
else:
return digui(n - 1) + 2 * (n - 1) + 1
while True:
n = int(input())
ans = 0
for i in range(1, n + 1):
ans += digui(i)
print(ans)
def digui(n):
if n <= 2:
return n - 1
else:
return digui(n - 1) + 2 ** (n - 2)
ans = 0
n = int(input())
for i in range(1,n+1):
ans += digui(i)
print(ans)
def digui():
num = input()
ans = int(num[::-1])
print(ans)
return digui()
digui()
step = 0
def move(n, a, b):
global step
step += 1
print(f"第{step}步:{n}号盘从{a}柱移至{b}柱")
def hanoi(n, a, b, c):
if n > 0:
hanoi(n - 1, a, c, b)
move(n, a, c)
hanoi(n - 1, b, a, c)
while True:
n = int(input())
hanoi(n, 'A', 'B', 'C')
step = 0
print()
最后要多一个换行
#include <stdio.h>
#define N 100000
int main()
{
long a;
long sum[N] = { 1, 2 };
for (a = 2; a < N; a++)
{
sum[a] = (sum[a - 1] + sum[a - 2]) % 1000000;
}
while (scanf("%ld", &a) != EOF)
{
printf("%ld\n", sum[a - 1]);
}
return 0;
}
这个题python过不了
n = int(input())
a = [0 for i in range(50)]
a[0] = 1
a[1] = 1
a[2] = 8
for i in range(3, 50):
a[i] = (a[i - 1] + a[i - 2]) * 3 - 5*a[i-3]
print(a[n-1])
思路:找数字的规律 用c/c++的话要用字符串存
while True:
n, m, k = map(int, input().split())
if n == 0 and m == 0 and k == 0:
break
exp = [0] * n
start = 1
i = 0
flag = 0
p = 1
ans = 0
while True:
exp[i] = start
if i == m - 1:
if exp[i] % 7 == 0 or '7' in str(exp[i]):
flag += 1
ans = exp[i]
if flag == k:
break
if i == n - 1:
p = -1
if i == 0:
p = 1
i += p
i %= n
start += 1
print(ans)
注意边界处理
while True:
n = int(input())
if n == 0:
break
ans = 0
p = 1
flag = 0
cnt = 0
while cnt < n:
flag += 1
pc = min(n - cnt, flag)
ans += pc * (2 ** (flag - 1))
cnt += pc
print(ans % 10000)
思路:打表找规律