(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?
题目大意:
三角形数序列中第 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的更多相关文章
- (Problem 2)Even Fibonacci numbers
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting w ...
- (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 ...
- (Problem 70)Totient permutation
Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...
- (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 ...
- (Problem 49)Prime permutations
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual ...
- (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 ...
- (Problem 36)Double-base palindromes
The decimal number, 585 = 10010010012(binary), is palindromic in both bases. Find the sum of all num ...
- (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 ...
- (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, ...
随机推荐
- 【iOS】Plist-XML-JSON数据解析
网络上数据传输通用的有XML.JSON等,iOS中也能够用Plist. 要进行传输数据.就要首先进行序列化: 1.序列化. 对象转换成二进制流.(这个一句话即可) .反序列化. 二进制流转换为对象等 ...
- Javascript基础篇小结
转载请声明出处 博客原文 随手翻阅以前的学习笔记,顺便整理一下放在这里,方便自己复习,也希望你有也有帮助吧 第一课时 入门基础 知识点: 操作系统就是个应用程序 只要是应用程序都要占用物理内存 浏览器 ...
- C# 第三方控件 下面的Item不显示了
当高版本的第三方版本 替换成低版本的第三方后,item,不显示了之后,请试着再次在这基础上添加一个Item,观察这个Item和原来已经在的却不显示的Item的区别在哪里.然后去源程序正常文件哪里 将这 ...
- js 数字
var text = $("#iptNum").val(); if(isNaN(text)){ alert("不是数字"); } else{ alert(&qu ...
- [转]oracle误删数据的恢复
与数据打交道,免不了会误删一些数据,之后还commit了,连回滚的机会都没了,而更糟糕的是你又没有备份,这种事终于在今天被我不幸的遇上了... 唯一一点值得欣慰的是,我删除表记录的时候,时间不长,一天 ...
- JS 代码调试经验总结(菜鸟必读)
前言:不知不觉写了很多,希望你能耐心看完这篇文章 任何一个编程者都少不了要去调试代码,不管你是高手还是菜鸟,调试程序都是一项必不可少的工作.一般来说调试程序是在编写代码之后或测试期修改Bug 时进行的 ...
- 公选网站作业4_2.php
序: 此实验的功能是只有已经登陆或者已经注册的用户才有发表留言的资格,否则只能够浏览留言. 1. 主要文件: 4_2login.html// 登陆的主页面 4_2login.php //处理登 ...
- 一步一步Asp.Net MVC系列_权限管理总结(附MVC权限管理系统源码)
在上一节中我们总结了关于权限控制的方式,我们这一节讲解关于权限控制中角色权限的授予处理等等并做本系列的总结. 首先,我们来谈谈权限控制中角色权限的控制,上一节只是针对权限拦截中比较粗的控制,如果我们需 ...
- The Use of Aliases in ElasticSearch
http://paulsabou.com/blog/2012/04/15/the-use-of-aliases-in-elasticsearch/ https://github.com/taskrab ...
- python 在 for i in range() 块中改变 i 的值的效果
先上一段代码: for i in range(3): i = 2 print(i) 实际结果是: 2 2 2 可以发现实际效果就是 在每次执行 for 语句块的内容后 i 会被重新赋值