中文
一品书生

生产工具决定生产效率


  • 首页

  • 归档

  • 关于我

  • 搜索

蓝桥杯算法练习

时间: 2019-12-07   |   分类: @未分类   | 字数: 430 字 | 阅读: 1分钟 | 阅读次数:

比赛信息

考题形式

  • 填空题 只要答案
  • 编程大题 要求只能使用编程解决问题,要尽可能考虑可行性和效率问题。

考察范围

入门篇

斐波那契函数(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=" ")

#暂无标签#
创作实属不易,如有帮助,那就打赏博主些许茶钱吧 ^_^
WeChat Pay

微信打赏

Alipay

支付宝打赏

第五项修炼 知识笔记
Python查缺补漏和实用技巧
  • 文章目录
  • 站点概览
一品书生

一品书生

品味人生

21 日志
15 分类
19 标签
GitHub 知乎
友情链接
  • Nutz
  • JFinal
  • Wendal
  • 廖雪峰
标签云
  • 暂无标签
  • 计划
  • Centos7
  • 思维方式
  • 总结
  • 英语学习
  • Firewall
  • Latex
  • Python
  • R
  • 比赛信息
    • 考题形式
    • 考察范围
  • 入门篇
    • 斐波那契函数(Fibonacci)
    • 圆的面积
    • 前n项和
    • 数列排序
© 2010 - 2021 一品书生
Powered by - Hugo v0.86.0 / Theme by - NexT
/
0%