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. Angular——内置服务

    $location <!DOCTYPE html> <html lang="en" ng-app="App"> <head> ...

  2. 4星|《DK商业百科》:主要商业思想与事件的概括

    全书分为以下6章:1:企业的起步与发展:2:领导和人力资源:3:理财:4:战略和运营:5:营销管理:6:生产与生产后.每章有拆分为成多个比较小的专题,阐述相关专题的主要的商业思想与实践. 基本是作者按 ...

  3. ansible 批量推送ssh秘钥

    ansible 批量推送ssh秘钥 参考:http://docs.ansible.com/ansible/authorized_key_module.html # vi /etc/ansible/ho ...

  4. jboss解决ip访问受限问题

    jboss启动后,localhost可以访问,127.0.0.1可以访问,但是内网ip却访问不了,比如ip是192.168.1.2,这个192.168.1.2就访问不到web页面 解决方案: jbos ...

  5. day21-1 类的继承

    目录 类的继承 什么是继承 为什么用继承 对象的继承 继承与抽象 继承的应用 对象属性查找顺序 类的继承 什么是继承 继承是一种创建新类的方式,新建的类可以继承一个或多个父类(python支持多继承) ...

  6. [HNOI2006]最短母串 (AC自动机+状压)

    Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. Input 第一行是一个正整数n(n<=12) ...

  7. 09C语言指针

    C语言指针 地址 地址就是数据元素在内存中的位置表示: &变量名 #include <stdio.h> int main(){ int aa; unsigned int bb = ...

  8. 11Java Server Pages 动作

    Java Server Pages 动作 JSP标准动作 分类 JSP标准动作 存取JavaBean相关 <jsp:useBean> <jsp:setProperty> < ...

  9. 12Microsoft SQL Server 索引

    Microsoft SQL Server 索引 8.1创建索引 CREATE INDEX idx_name ON table_name(列名) --创建非聚集索引 use student go cre ...

  10. 【转载】appium自动化环境搭建

    1.java开发环境JDK 2.android SDK(platform/platform tools/tools/build tools) 3.python下载安装(pip) 4.appium下载安 ...