日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python CheckiO 题解】Count Consecutive Summers

發布時間:2023/12/10 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python CheckiO 题解】Count Consecutive Summers 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

CheckiO 是面向初學者和高級程序員的編碼游戲,使用 Python 和 JavaScript 解決棘手的挑戰和有趣的任務,從而提高你的編碼技能,本博客主要記錄自己用 Python 在闖關時的做題思路和實現代碼,同時也學習學習其他大神寫的代碼。

CheckiO 官網:https://checkio.org/

我的 CheckiO 主頁:https://py.checkio.org/user/TRHX/

CheckiO 題解系列專欄:https://itrhx.blog.csdn.net/category_9536424.html

CheckiO 所有題解源代碼:https://github.com/TRHX/Python-CheckiO-Exercise


題目描述

【Count Consecutive Summers】:一個正整數可以用幾個連續的正整數之和來表示,例如,正整數42,可以有四種方法來表示:(1)3 + 4 + 5 + 6 + 7 + 8 + 9、(2)9 + 10 + 11 + 12、(3)13 + 14 +15、(4)42,其中第四種方法表示該正整數僅由其本身組成,你的任務是計算有多少種表示方法。

【鏈接】:https://py.checkio.org/mission/count-consecutive-summers/

【輸入】:整數

【輸出】:整數

【前提】:輸入始終是一個正整數

【范例】

count_consecutive_summers(42) == 4 count_consecutive_summers(99) == 6

代碼實現

def count_consecutive_summers(num):n = 0for i in range(1,num+1):m = iwhile m < num:i,m = i+1,m+i+1if m == num:n += 1return nif __name__ == '__main__':print("Example:")print(count_consecutive_summers(42))# These "asserts" are used for self-checking and not for an auto-testingassert count_consecutive_summers(42) == 4assert count_consecutive_summers(99) == 6assert count_consecutive_summers(1) == 1print("Coding complete? Click 'Check' to earn cool rewards!")

大神解答

大神解答 NO.1

count_consecutive_summers = lambda n: sum(not n%k for k in range(1, n+1, 2))

大神解答 NO.2

def count_consecutive_summers(num):# s + (s + 1) + (s + 2) + ... + (s + n)# = (n + 1) * s + n * (n + 1) / 2 = num# s = (num - n * (n + 1) / 2) / (n + 1)# we need denominator of s is divisible by n + 1count = 0for n in range(0, num): # we move nd = num - n * (n + 1) // 2 # calculate denominatorif d <= 0: breakif d % (n + 1) == 0: # s is integer?count += 1return count

大神解答 NO.3

def count_consecutive_summers(num):ways = 0for i in range(1, num + 1):n = 1 - 2 * i + ((2 * i - 1) ** 2 + 8 * num) ** (1/2)if n % 1 == 0:ways += 1return ways

大神解答 NO.4

count_consecutive_summers=lambda n:sum([n%b*2==b-b%2*b for b in range(1,int((2*n)**.5)+1)]) """ Is this really working? Yes it is, it's one of the shortest code to solve the problem and it's doing it very efficiently as well2nd version ----------- How does it work: sum from p to p+a when p and a are integers is (2p+a)*(a+1)/2 so n=(2p+a)*(a+1)/2 so 2p+a=2n/(a+1) finally p=(2n-a-a^2)(2*(a+1)) replacing a+1 with b p=(2n-b(b-1))/2b and we know that p should b an integer, which means that 2n-b(b-1)%(2b)==0 or 2n%(2b)==b(b-1)%(2b) Left part can be reduce to n%b*2 Right part can be reduce to (b-1)%2*b which can be written b-b%2*b for 2 characters lessValues for a are from a=0 (sum of a single value) to a=0.5*(-1+sqrt(1+8*n)) when we have the lonest sum of numbers This value does not make a nice 'golf' code, but 0.5*(-1+sqrt(1+8*n)) is just slightly smaller than sqrt(2n) for n=1 000 000, this means that the program will evaluate a up to 1414 instead of 1413, acceptable trade off for a shorter code :) Values for b are therefore from 1 to sqrt(2n)+12nd version uses: - b in place of a+1 for a lot of save characters - modulo instead of int(x)==x to define if x is integer, greatly save chars but it's much faster (20-30%)It was a great exerice so found a way to solve it for both code size and speed """

總結

以上是生活随笔為你收集整理的【Python CheckiO 题解】Count Consecutive Summers的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。