Problem 42
Problem 42
https://projecteuler.net/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.
讲单词中的字母转换为字母表中的位置(如:A=1),将它们相加。如果得到的数字是一个三教数,则这个单词为三角单词。
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?
找找这两千多个英语常用词中有多少个三角单词?
filename = 'p042_words.txt'
with open(filename) as f:
data = f.read() words = data.split(',')
for i in range(len(words)):
words[i] = words[i][1:-1] triangle_nums = [i * (i + 1) // 2 for i in range(1, 100)]
triangle_words = []
alphabet = [chr(i) for i in range(65, 65 + 26)]
word_value = 0
count = 0
for word in words:
for letter in word:
word_value += alphabet.index(letter) + 1
if word_value in triangle_nums:
triangle_words.append(word)
count += 1
word_value = 0
else:
word_value = 0 print(triangle_words)
print(count)
Problem 42的更多相关文章
- (Problem 42)Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- leetcode problem 42 -- Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- Project Euler:Problem 42 Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- EularProject 42:单词解码出来的三角形数
Coded triangle numbers Problem 42 The nth term of the sequence of triangle numbers is given by, tn = ...
- python获取字母在字母表对应位置的几种方法及性能对比较
python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...
- UVa 106 - Fermat vs Pythagoras(数论题目)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
- 《DSP using MATLAB》Problem 8.42
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- Problem F: 零起点学算法42——多组测试数据输出II
#include<stdio.h> int main() { ; while(scanf("%d%d%d",&a,&b,&c)!=EOF) { ...
随机推荐
- J - 玩游戏
小A和小B玩游戏,初始的时候小A给小B一组包含n个数的数组.他们按如下的规则进行: 每次小B得到一组数,他把这组数的和加到自己的分数里面(他的初始分数是0),然后他把这组数还给小A. 如果小A得到的这 ...
- Codeforces Beta Round #67 (Div. 2)C. Modified GCD
C. Modified GCD time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- YTU 2797: 复仇者联盟之关灯
2797: 复仇者联盟之关灯 时间限制: 1 Sec 内存限制: 128 MB 提交: 563 解决: 160 题目描述 输入n(1~500)盏灯并编号,输入1~9(包含1和9)的数字m,灭掉编号 ...
- 【POJ 2777】 Count Color
[题目链接] http://poj.org/problem?id=2777 [算法] 线段树 [代码] #include <algorithm> #include <bitset&g ...
- mst
https://www.zybuluo.com/ysner/note/1245941 题面 给一个\(n\)点完全图,点权均小于\(2^m\).定义边权等于两端点点权的与和(即\(a_i\&b ...
- E20170916-hm
sassy adj. 无礼的; 漂亮的; <非正,美> <贬>粗鲁的; <褒>时髦的; digest vt. 消化; 整理; compressor n. 压气 ...
- [Swift通天遁地]七、数据与安全-(19)使用Swift实现原生的SHA1加密
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Windows(7/8/10)搭建kibana 6.x版本(elasticsearch的可视化服务)
在搭建kibana之前,我们先了解下什么是kibana Kibana 是一款开源的数据分析和可视化平台,它是 Elastic Stack 成员之一,设计用于和 Elasticsearch 协作.您可以 ...
- ajax 以json 的形式来传递返回参数的实例
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestWcf.aspx.c ...
- 绑定树tree 的后台方法
#region 获取部门列表树集合 /// <summary> /// 获取部门列表树集合 /// </summary> ...