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?

题目大意:

三角形数序列中第 n 项的定义是: tn = ½n(n+1); 因此前十个三角形数是:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

通过将一个单词中每个字母在字母表中的位置值加起来,我们可以将一个单词转换为一个数。例如,单词SKY的值为19 + 11 + 25 = 55 = t10。如果单词的值是一个三角形数,我们称这个单词为三角形单词。

words.txt (右键另存为)是一个16K的文本文件,包含将近两千个常用英语单词。在这个文件中,一共有多少个三角形词?

//(Problem 42)Coded triangle numbers
// Completed on Tue, 19 Nov 2013, 03:34
// Language: C11
//
// 版权所有(C)acutus (mail: acutus@126.com)
// 博客地址:http://www.cnblogs.com/acutus/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h> bool test(int n)
{
int m;
m = (int)sqrt(n * );
if(m * (m + ) == * n) return true;
else return false;
} int count(char * s)
{
int i = ;
int sum = ;
while(s[i] != '\0') {
sum += s[i] - 'A' + ;
i++;
}
return sum;
} void solve(void)
{
FILE *fp;
int i, j, k;
char *s, c;
int sum = ;
char a[]; fp = fopen("words.txt", "r");
fseek(fp, , SEEK_END);
int file_size;
file_size = ftell(fp);
fseek(fp, , SEEK_SET);
s = (char*)malloc(file_size * sizeof(char));
fread(s, sizeof(char), file_size, fp); i = j = k = ;
while(i <= file_size) {
c = s[i++];
if(!isalpha(c)) {
if(c == ',') {
j = ;
if(test(count(a))) sum++;
memset(a,'\0', * sizeof(char));
}
} else {
a[j++] = c;
}
}
if(test(count(a))) sum++;
memset(a,'\0', * sizeof(char)); printf("%d\n",sum);
} int main(void)
{
solve();
return ;
}
Answer:
162

(Problem 42)Coded triangle numbers的更多相关文章

  1. (Problem 2)Even Fibonacci numbers

    Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting w ...

  2. (Problem 21)Amicable numbers

    Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into ...

  3. (Problem 70)Totient permutation

    Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...

  4. (Problem 74)Digit factorial chains

    The number 145 is well known for the property that the sum of the factorial of its digits is equal t ...

  5. (Problem 49)Prime permutations

    The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual ...

  6. (Problem 47)Distinct primes factors

    The first two consecutive numbers to have two distinct prime factors are: 14 = 2  7 15 = 3  5 The fi ...

  7. (Problem 36)Double-base palindromes

    The decimal number, 585 = 10010010012(binary), is palindromic in both bases. Find the sum of all num ...

  8. (Problem 34)Digit factorials

    145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are ...

  9. (Problem 29)Distinct powers

    Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, ...

随机推荐

  1. Android学习路线(六)为Android应用加入ActionBar

    Action bar 是你可以为你的应用的Activity实现的最为重要的设计元素之中的一个.它提供了集中UI特性.而且通过提供和其它的Android应用的一致性体验让你的应用可以非常快被用户熟悉.基 ...

  2. jboss7.1.1配置数据库mysql与oracle

    环境: joss7.1.1安装成功,路径为:D:\profession\jboss-as-7.1.1.Final mysql-connector-java-5.1.18-bin.jar(mysql 5 ...

  3. hbase 单机安装问题

    报zookeeper exception not found I fixed this by editing the file "/usr/local/hbase-0.94.1/conf/h ...

  4. js添加删除元素

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  5. hibernate 及缓存机制

    hibernate 是一个持久层的框架,经常访问物理数据库,为了降低应用程序访问物理数据库的频次, 从而提升性能, hibernate缓存机制分为: 一类是session 级缓存,二是sessionF ...

  6. Control的Invoke和BeginInvoke详解

    (一)Control的Invoke和BeginInvoke 我们要基于以下认识: (1)Control的Invoke和BeginInvoke与Delegate的Invoke和BeginInvoke是不 ...

  7. HiveQL与SQL区别

    转自:http://www.aboutyun.com/thread-7327-1-1.html 1.Hive不支持等值连接 SQL中对两表内联可以写成:select * from dual a,dua ...

  8. 《JavaScript+DOM编程艺术》的摘要(三)---图片库实例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  9. 关于微信浏览不能URL传参,URL中的问号被删除

    关于微信浏览不能URL传参,URL中的问号被删除. 尼玛难道没有人遇到过这个问题?看了微信支付SDK3.0的代码,看到urlencode 看到了几次.实际上 在微信浏览器下 header('locat ...

  10. Mysql笔记之 -- replace()实现mysql 替换字符串

    mysql 替换函数replace()实现mysql 替换字符串 mysql 替换字符串的实现方法:  mysql中replace函数直接替换mysql数据库中某字段中的特定字符串,不再需要自己写函数 ...