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开发环境后.用到下面几个命令.android, adb, emulator android - 最主要的android命令.能够进行sdk更新,列出设备源,生成虚拟设备等. adb ...

  2. sql server 2005 表master..spt_values

    IF OBJECT_ID('tempdb..#t') IS NOT NULL DROP TABLE #tGOcreate table #t(id int identity,Dt varchar(10) ...

  3. 使用CSS3伸缩盒实现图片垂直居中

    用CSS实现图片垂直居中的方法有很多,针对移动端设备可以用CSS3伸缩盒来实现图片垂直居中. 代码如下: <div class="box"> <img src=& ...

  4. java——HashCode和equal方法

    equals()反映的是对象或变量具体的值,即两个对象里面包含的值--可能是对象的引用,也可能是值类型的值. 而hashCode()是对象或变量通过哈希算法计算出的哈希值. 之所以有hashCode方 ...

  5. 使用mobile jQuery 动态给select下拉添加数据,选中项默认不显示的解决方法。

    getaddress(); function getaddress(type=0,parent='') { var tid=1; $.ajax({ type: "post", ur ...

  6. Android Log工具类

    import java.text.SimpleDateFormat; import java.util.Date; import android.util.Log; public class LogU ...

  7. 对discuz的代码分析学习(三)mysql驱动

    一. 属性分析         1.tablepre :表名称前缀        2.version : 数据库版本        3.drivertype:驱动类型        4.querynu ...

  8. linux服务器在运行210天左右宕机

    减小字体 增大字体 作者:错新网  来源:www.cuoxin.com  发布时间:2014-2-25 19:21:32 错新网讯   最近几天,一批linux线上的服务器接连宕机,当时以为是硬件问题 ...

  9. 射频识别技术漫谈(28)——基于MF1射频卡的酒店门锁设计

    电子门锁是现代星级酒店管理电子化.智能化的重要电子设备.相较于传统的机械锁,基于RFID技术的电子门锁使用方便,易于管理,安全性高,可实现对开锁用户的分优先级自动管理,对房间入住信息实现自动统计与报表 ...

  10. delphi 7 信息对话框的按钮屏蔽键盘操作,只允许鼠标点击

    本问题由 delphi 学友QQ群中一位群友提出,个人觉得是一个好问题. 本教程源码下载 本教程面向新手,希望大家能从中学到除了本功能之外的真正编程技能. 本功能的实现原理,用窗口当对话框的界面,在界 ...