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. CNN:测试一下YoloV3

    项目地址:https://pjreddie.com/darknet/yolo/ mAP提升了不少,在VS上试一把 V3 的权值: https://pjreddie.com/media/files/yo ...

  2. CAD如何设置系统变量

    主要用到函数说明: MxDrawXCustomFunction::Mx_SetSysVar 设置系统变量.详细说明如下: 参数 说明 CString sVarName 系统变量名 Value 需要设置 ...

  3. oracle 安装准备

    1.选择数据库 (官网查询支持的操作系统) 2.选择系统 (官网查询支持的硬件)(更新补丁) 3.选择硬件 (io性能测试--oracle 大量小文件读写) 4.oracle 升级(和打补丁) 5.o ...

  4. Controller传值到前端页面的几种方式

    一丶追加字符串传值 #region 02-追加字符串传值 /// <summary> /// 02-追加字符串传值 /// </summary> /// <returns ...

  5. Python之实例属性和类属性

    参考原文 廖雪峰Python 实例属性和类属性 在前面已经说过由于Python是动态语言,可以根据类的实例绑定任何的属性. 给实例绑定属性的方法是通过实例变量,或者self变量绑定的: class S ...

  6. TWaver GIS制作穹顶之下的雾霾地图

    “我不满意,我不想等待,我也不再推诿,我要站出来做一点什么.我要做的事,就在此时,就在此刻,就在此地,就在此生”.自离职央视后,沉寂许久的知名记者.主持人柴静昨日携个人视频新作 <穹顶之下> ...

  7. Python&机器学习总结(一)

    ① numpy中np.c_和np.r_ np.r_是按列连接两个矩阵,就是把两矩阵上下相加,要求列数相等,类似于pandas中的concat(). np.c_是按行连接两个矩阵,就是把两矩阵左右相加, ...

  8. numpy.random模块常用函数解析

    numpy.random模块中常用函数解析 numpy.random模块官方文档 1. numpy.random.rand(d0, d1, ..., dn)Create an array of the ...

  9. Python关于导入模块的一些感想:

    写项目的时候,碰到这种情况 程序业务为core,里面有两个目录,core1 和core2  core1中有三个模块,business  main   main1 程序入口为bin目录下的project ...

  10. 配置Struts2后运行jsp出现404的解决方法

    更新:善用控制台查看错误信息 --------------------------------------------- 原因:Java Build Path没有导入正确的jar包或者导入了但没有把相 ...