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. MySql备份表数据

    一:根据user表创建user_backup表 drop table if exists user_backup; create table user_backup like user; // lik ...

  2. PendingIntent 显示通知

    安卓显示通知 PendingIntent pendingIntent=PendingIntent.getActivity(Media.this,0, new Intent(Media.this,Med ...

  3. spark查看stage和tasks信息

    spark提供了web-ui接口.外部命令等多种方法监视spark程序的执行状态.利用spark的监视功能,可以方便的查看spark应用程序执行的状态,具体包括:1)stage和tasks列表信息  ...

  4. 通俗理解LDA主题模型(boss)

    0 前言 看完前面几篇简单的文章后,思路还是不清晰了,但是稍微理解了LDA,下面@Hcy开始详细进入boss篇.其中文章可以分为下述5个步骤: 一个函数:gamma函数 四个分布:二项分布.多项分布. ...

  5. js判断是安卓 还是 ios webview

    判断原理:JavaScript是前端开发的主要语言,我们可以通过编写JavaScript程序来判断浏览器的类型及版本.JavaScript判断浏览器类型一般有两种办法,一种是根据各种浏览器独有的属性来 ...

  6. GetArxPath

    extern HINSTANCE _hdllInstance;CString GetArxPath(){ CString strArxPath; GetModuleFileName(_hdllInst ...

  7. 第二节:1_C#中的委托的使用和讲解(转)

    C# 中的委托 引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容 ...

  8. Python&机器学习总结(二)

    ① Python中的Sort Python中的内建排序函数有 sort()和sorted()两个 list.sort(func=None, key=None, reverse=False(or Tru ...

  9. linux more-显示文件内容,每次显示一屏

    博主推荐:获取更多 linux文件内容查看命令 收藏:linux命令大全 more命令是一个基于vi编辑器文本过滤器,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作.more名单中 ...

  10. CCF201612-2 工资计算 java(100分)

    试题编号: 201612-2 试题名称: 工资计算 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 小明的公司每个月给小明发工资,而小明拿到的工资为交完个人所得税之后的工资.假 ...