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 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的更多相关文章
- Project Euler:Problem 61 Cyclical figurate numbers
Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygon ...
- Project Euler 42 Coded triangle numbers
题意:三角形数序列的第n项由公式tn = 1/2n(n+1)给出:因此前十个三角形数是: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, - 将一个单词的每个字母分别转化为其 ...
- Project Euler:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Project Euler:Problem 58 Spiral primes
Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length ...
- 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 ...
随机推荐
- Android Camera 3D效果
一.概念 在Android中要想实现3D效果,第一个想到的应该就是OpenGL ES,因为在很多基础教材中几乎都提到了它.但是其使用起来还是稍微麻烦一些,而且它也主要用在游戏方面,那在应用方面有没有更 ...
- Selenium示例集锦--常见元素识别方法、下拉框、文本域及富文本框、鼠标操作、一组元素定位、弹窗、多窗口处理、JS、frame、文件上传和下载
元素定位及其他操作 0.常见的识别元素的方法是什么? driver.find_element_by_id() driver.find_element_by_name() driver.find_ele ...
- PAT1021Deepset Root
题意: 连通则输出最深点.第一步找某个点的最深的,然后从这个最深的点查找其他最深点,做并集. 不连通则输出连通图个数. #include<iostream> #include<cst ...
- Windows开启ICMP包回显
- Codeforces_758_D_(区间dp)
D. Ability To Convert time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 08Oracle Database 完整性约束
Oracle Database 完整性约束 非空约束 创建表时 Create table table_name( Column_name datatype NOT NULL,… ); 修改表时 Alt ...
- 在MONO Design中使用Flex3D
在项目开发组的努力下,HTML5 3D 的开发包变得越来越优秀,越来越健壮:基于HTML5 3D技术的MONO Design建模平台功能也变得越来越强大和完善,这个方便了很多使用我们HTML5 3D的 ...
- Makefile,Shell command,Shell Language 之间的联系
1. Makefile 首先要知道Makefile 是什么东西,Makefile 是一个指令文件,里面存储着自定义的命令(可以借助已有的命令创造而来)在不同的系统下对Makefile 的区别不一样,L ...
- 抓包工具的感触(charles and fiddler)
最近测mobile,一直徘徊在fiddler 和 charles之间: charles 的证书装了 ,才能正常抓包: 后来因为重定向,分享到扣扣,微信的跳转功能,跳转到wap 或者跳转到PC 或者跳 ...
- (四)Python3 循环语句——for
for循环的一般格式如下: for <variable> in <sequence>: <statements> else: <statements> ...