比赛信息
考题形式
- 填空题 只要答案
- 编程大题 要求只能使用编程解决问题,要尽可能考虑可行性和效率问题。
考察范围
入门篇
斐波那契函数(Fibonacci)
+name: Fibonacci
class Fibonacci:
@staticmethod
def Recursion(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return __class__.Recursion(n-1) + __class__.Recursion(n-2)
@staticmethod
def GeneralFor(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
pre = 0
next = 1
for _ in range(1,n):
result = pre + next
pre = next
next = result
return result
@staticmethod
def GeneralWhile(n):
if n == 0:
return 0
if n == 1:
return 1
pre = 0
next = 1
while n >= 2:
n -=1
result = pre + next
pre = next
next = result
return result
@staticmethod
def Python(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
pre = 0
next = 1
for _ in range(1,n):
result = pre + next
pre, next = next, result
return result
print(Fibonacci.Recursion(8))
print(Fibonacci.GeneralFor(8))
print(Fibonacci.GeneralWhile(8))
print(Fibonacci.Python(8))
# n=int(input())
f=[1,1,1]
if n>2:
for i in range(3,n+1):
f.append((f[i-1]+f[i-2])%10007)
print(f[n])
圆的面积
from decimal import Decimal
from math import pi
# r = input()
r = Decimal(r)
area = r**2*Decimal(pi)
print(area.quantize(Decimal('0.0000000'), rounding="ROUND_HALF_UP"))
前n项和
# n = int(input())
sumN = n + (n*(n-1))/2
print(int(sumN))
数列排序
冒泡算法
之前的数和之后的数循环比较,如果之前的数大于之后的书,交换位置,否则不换位置。比较次数是数列长度-1次。
def bubbleSort(nums):
for sums in range(len(nums) - 1): # 遍历 len(nums)-1 次
for pre in range(len(nums) - sums - 1): # 已排好序的部分不用再次遍历
next_ = pre + 1
if nums[pre] > nums[next_]:
nums[pre], nums[next_] = nums[next_], nums[pre] # Python 交换两个数不用中间变量
return nums
n = int(input())
n_txt = input()
n_list = [int(i) for i in n.split()][:n]
for i in n_list[::-1]:
print(i,end=" ")
import collections
n = int(input())
n_txt = input().split()
n_list = collections.deque([])
for i in range(n):
n_list.append(int(n_txt[i]))
for i in range(n):
print(n_list.pop(),end=" ")