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?

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
using namespace std; map<int, int>mp;
void triangle()
{
for (int i = 1; i < 10000; i++)
{
mp[i*(i + 1) / 2] = 1;
}
} int main()
{
triangle();
ifstream input;
input.open("words.txt");
char s[16347];
while (!input.eof())
input >> s;
input.close();
vector<string> name;
string tm;
for (int i = 1; i <= 16346; i++)
{
if (s[i] >= 65 && s[i] <= 90 && s[i + 1] == '"')
{
tm = tm + s[i];
name.push_back(tm);
tm.clear();
}
else if (s[i] == ',' || s[i] == '"')
continue;
else
tm = tm + s[i];
}
int ans = 0;
for (int i = 0; i < name.size(); i++)
{
int sum = 0;
for (int j = 0; j < name[i].length(); j++)
{
sum = sum + name[i][j] - 'A' + 1;
}
if (mp[sum] == 1)
ans++;
}
cout << ans << endl;
system("pause");
return 0;
}

Project Euler:Problem 42 Coded triangle numbers的更多相关文章

  1. Project Euler:Problem 61 Cyclical figurate numbers

    Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygon ...

  2. Project Euler 42 Coded triangle numbers

    题意:三角形数序列的第n项由公式tn = 1/2n(n+1)给出:因此前十个三角形数是: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, - 将一个单词的每个字母分别转化为其 ...

  3. Project Euler:Problem 55 Lychrel numbers

    If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...

  4. Project Euler:Problem 88 Product-sum numbers

    A natural number, N, that can be written as the sum and product of a given set of at least two natur ...

  5. Project Euler:Problem 87 Prime power triples

    The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...

  6. Project Euler:Problem 89 Roman numerals

    For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...

  7. Project Euler:Problem 93 Arithmetic expressions

    By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and making use of the four ari ...

  8. Project Euler:Problem 58 Spiral primes

    Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length ...

  9. Project Euler:Problem 39 Integer right triangles

    If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exact ...

随机推荐

  1. js获取地址栏参数2种最简单方法

    NO1:(本人最喜欢) //普通参数 function GetQueryString(name) { var reg = new RegExp("(^|&)"+ name ...

  2. 设置vscode为中文

    设置vscode为中文 ctr+shift+p 输入 configure language 进 en更改为zh-cn , 重启vscode即可 , 如果还不行,就安装插件

  3. IDEA中springboot项目打包成jar

      springboot的打包方式有很多种.有打成war的,有打成jar的,也有直接提交到github,通过jekins进行打包部署的.这里主要介绍如何打成jar进行部署.不推荐用war,因为spri ...

  4. 02C++基本语法

    基本语法 2.1.1单行注释 // 2.1.2多行注释 /* * */ 2.1.3标识符 C++ 标识符是用来标识变量.函数.类.模块,或任何其他用户自定义项目的名称.一个标识符以字母 A-Z 或 a ...

  5. 参考整理papers(一)

    https://blog.csdn.net/qq_14845119/article/details/82219246 整理了OCR的论文,可以参考一下.还有一些相关论文 论文(poster):Scen ...

  6. contab的使用方法

    linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另 外, 由于使用者自己也可以设置计划任务,所以, ...

  7. valgrind检查代码内存泄漏,5种内存泄漏情况

    摘要: valgrind是linux下用于调试程序和查找内存泄露的常用工具.valgrind会报告5种内存泄露,"definitely lost", "indirectl ...

  8. JPA @MappedSuperclass注解

    该注解只能引用于类上,使用该注解的类将不是一个完整的类,不会映射到数据库的表中,但是该类的属性会映射到其子类的数据库字段中 @MappedSuperclass注解使用在父类上面,是用来标识父类的作用 ...

  9. TWaver动画之雷达扫描效果

    UI和功能是好的产品的两个重要因素,很多产品往往只注重功能上的设计,而忽略了UI.在这个“看脸”的时代,就算产品的功能很强大,如果UI跟不上步伐,你的产品都会在客户心中大打折扣.做安全和监控的项目中经 ...

  10. XML中的特殊(保留)字符数据

    XML中的特殊(保留)字符数据 制作人:全心全意 在XML文档中,有些字符会被XML解析器当作标记进行处理.如果希望把这些字符作为普通字符处理,就需要使用实体引用或CDATA段. 使用实体引用 为了避 ...