(Problem 42)Coded triangle numbers

时间:2022-04-04 20:29:57

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 提供了API可获取到系统相册中的一些信息,主要还是通过ContentProvider 来获取想要的内容. 代码很简单,只要熟悉ContentProvider 就可以了. public ...

  2. Myeclipse8&period;5 最新注册码以使用方法(可以用到2015年!!!)

    已破解的一组,复制即可!(注册码到2015年哦!) name:LIKEcode:YLR8ZC-855550-6067725176540043 使用方法:把注册码贴到Window-->prefer ...

  3. 战胜C语言中令人头疼的问题

    C语言一共32个关键字,下面一一列出:   1.auto声明自动变量 在默认情况下,编译器默认所有变量都是auto 2.int声明整型变量 3.double声明双精度变量 4.long声明长整型变量 ...

  4. Ubuntu14&period;04LTS安装记录(办公室联想台式机)

    一.用UltraISO制作U盘启动器,被安装的电脑要设置成从U盘启动. 二.傻瓜式安装简体中文版 三.安装更新 sudo apt-get update sudo apt-get upgrade 四.安 ...

  5. 函数 xdes&lowbar;get&lowbar;descriptor&lowbar;with&lowbar;space&lowbar;hdr

    获得区描述符 xdes entry 的offset /********************************************************************//** ...

  6. 浅谈标签构建——TagBuilder

    在很多项目中,可能我们需要写一些通用的控件标签,今天来简单的学习一下吧. 在前文中已经学习了 如何自定义MVC控件标签 ,感兴趣的朋友可以去看看. 今天主要还是讲解一下TagBuilder 我们打开源 ...

  7. hdu 4117 GRE Words (ac自动机 线段树 dp)

    参考:http://blog.csdn.net/no__stop/article/details/12287843 此题利用了ac自动机fail树的性质,fail指针建立为树,表示父节点是孩子节点的后 ...

  8. 使用handler倒计时

    package com.example.jikangwang.myapplication; import android.content.Intent; import android.os.Handl ...

  9. redhat 6&period;5安装ansible

    安装epel 源: rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm 安装ansible ...

  10. 使用LeakCanary检测内存泄露 翻译 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...