EularProject 42:单词解码出来的三角形数

Coded triangle numbers

Problem 42

The nth term of the sequence of triangle numbers is given by, tn = ?n(n+1); so the first ten triangle numbers are:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …

By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.

Using words.txt (right click and ‘Save Link/Target As…’), a 16K text file containing nearly two-thousand common English words, how many are triangle words?

Answer:

162

Completed on Thu, 30 Jul 2015, 09:06

from math import sqrt

def IsTriangleNumber(word):
    data=0
    for i in word:
        data+=ord(i)-ord(‘A‘)+1
    k=int(sqrt(data*2))
    if data*2==k*(k+1):
        return 1
    else:
        return 0

count=0
for line in open(‘p042_words.txt‘,‘r‘):
    line=line.strip(‘\n‘).split(",")
    for word in line:
        word=word.strip("\"")
        count+=IsTriangleNumber(word)

print(count)
时间: 2024-08-06 09:58:54

EularProject 42:单词解码出来的三角形数的相关文章

hdu 5312 Sequence(数学推导——三角形数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1336    Accepted Submission(s): 410 Problem Description Today, Soda has learned a

三角形数

三角形数 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte 总提交:460            测试通过:114 描述 一定数目的点或圆在等距离的排列下可以形成一个等边三角形,这样的数被称为三角形数.比如10个点可以组成一个等边三角形,因此10是一个三角形数: x x x x x x x x x x 开始18个三角形数是1.3.6.10.15.21.28.36.45.55.66.78.91.105.120.136.153.171. 请

三角形数且是完全平方数

三角形数:an=n*(n+1)/2; 完全平方数:bn=c^2; 既是三角形数又是完全平方数:An=6*A(n-1)-A(n-2)+2; A[23]={ 0, 1, 8, 49, 288, 1681, 9800, 57121, 332928, 1940449, 11309768, 65918161, 384199200, 2239277041, 13051463048, 76069501249, 443365544448, 2584123765441, 15061377048200, 87784

剑指offer (42) 单词翻转

题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符顺序不变 题解分析: 两次翻转: 第一次翻转整个句子 第二次解析出每个单词并将单词翻转 void reverse(char* first, char* last) { assert(first != NULL && last != NULL); while (first < last) { char temp = *first; *first = *last; *last = temp; ++first; --last; }

HDU 5312 Sequence(三角形数应用)——BestCoder 1st Anniversary ($)

传送门 Sequence Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1424    Accepted Submission(s): 442 Problem Description Today, Soda has learned a sequence whose n-th (n≥1) item is 3n(n?1)+1. Now

hdu 5312 Sequence 三角形数

这道题可以用三角形数的性质解出来,所谓三角形数就是n*(n-1)/2其中n>2.就是1,3,6,10--,也可以表示为 n*(n+1)/2...性质:任何一个正整数最多用三个三角形数就可以表示出来,这道题是3*n*(n-1)+1,就可以 表示为6*(n*(n-1)/2)+1,假如m需要k(k>=3)个数来表示,就相当于6*(k个三角形数的和)+k = m:所以 只要判断(m-k)%6是否等于0就可以了.需要一个或两个数的时候需要特判一下.通过这道题了解了三角形数的这个神性质 贴代码: #inc

hdu 5312 三角形数 二分查找

Sequence Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 891    Accepted Submission(s): 271 Problem Description Today, Soda has learned a sequence whose n-th(n≥1) item is 3n(n?1)+1. Now he wa

HDU 5312-Sequence(三角形数+推导)

题目地址:HDU 5312 题意:Soda习得了一个数列, 数列的第nn (n \ge 1)(n≥1)项是3n(n-1)+13n(n?1)+1. 现在他想知道对于一个给定的整数mm, 是否可以表示成若干项上述数列的和. 如果可以, 那么需要的最小项数是多少?例如, 22可以表示为7+7+7+17+7+7+1, 也可以表示为19+1+1+119+1+1+1. 思路: 三角形数形如n*(n-1)/2,他们形成的数列是1,3,6,10.......,同样也可以表示成n*(n+1)/2(表示方式不同).

使用FAAD库解码AAC实例及 及 faad解码后的通道数不正确的问题

使用FAAD解码AAC音频为PCM数据流程可以参考下面的文章 http://blog.csdn.net/gavinr/article/details/6959198#reply /** * faaddec.c * use faad library to decode AAC, only can decode frame with ADTS head */ #include <stdio.h> #include <memory.h> #include "faad.h&quo