Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938  53 = 49714.

What is the total of all the name scores in the file?

题目大意:

文件names.txt (右键另存为)是一个46K大小的文本文件,包含5000多个英文名字。利用这个文件,首先将文件中的名字按照字母排序,然后计算每个名字的字母值,最后将字母值与这个名字在名字列表中的位置相乘,得到这个名字的得分。

例如将名字列表按照字母排序后, COLIN这个名字是列表中的第938个,它的字母值是3 + 15 + 12 + 9 + 14 = 53。所以COLIN这个名字的得分就是938  53 = 49714.

文件中所有名字的得分总和是多少?

//(Problem 22)Names scores
// Completed on Mon, 18 Nov 2013, 15:03
// Language: C
//
// 版权所有(C)acutus (mail: acutus@126.com)
// 博客地址:http://www.cnblogs.com/acutus/#include <stdio.h>
#include <ctype.h>
#include <stdlib.h> struct str{
char name[];
}; char a[]; int namevalue(char *s)
{
int i, sum;
i = sum = ;
while(s[i]) {
sum += (s[i] - 'A' + );
i++;
}
return sum;
} int cmp(const void *a, const void *b)
{
return strcmp((*(struct str*)a).name, (*(struct str*)b).name);
} void solve(void)
{
FILE *fp;
int i, j, k;
char *s, c;
long long sum = ; struct str namestring[]; fp = fopen("names.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 = ;
strcpy(namestring[k++].name, a);
memset(a,'\0', * sizeof(char));
}
} else {
a[j++] = c;
}
}
strcpy(namestring[k].name, a);
memset(a,'\0', * sizeof(char)); qsort(namestring, , sizeof(namestring[]),cmp); for(i = ; i <= ; i++) {
sum += (namevalue(namestring[i].name) * (i + ));
}
printf("%lld\n",sum);
} int main(void)
{
solve();
return ;
}
Answer:
871198282

(Problem 22)Names scores的更多相关文章

  1. (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, ...

  2. (Problem 28)Number spiral diagonals

    Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...

  3. (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 ...

  4. (Problem 15)Lattice paths

    Starting in the top left corner of a 22 grid, and only being able to move to the right and down, the ...

  5. 基于Web的企业网和互联网的信息和应用( 1194.22 )

    基于Web的企业网和互联网的信息和应用( 1194.22 ) 原文更新日期: 2001年6月21日原文地址: http://www.access-board.gov/sec508/guide/1194 ...

  6. (Problem 73)Counting fractions in a range

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

  7. (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 triangl ...

  8. (Problem 41)Pandigital prime

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  9. (Problem 70)Totient permutation

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

随机推荐

  1. iOS搜索指定字符在字符串中的位置

    NSString *tmpStr = @"asd341234aaaaccd"; NSRange range; range = [tmpStr rangeOfString:@&quo ...

  2. GLView基本分析

    GLView是cocos2d-x基于OpenGL ES的调用封装UI库. OpenGL本身是跨平台的计算机图形实现API,在每一个平台的详细实现是不一样.所以每次使用之前先要初始化,去设置平台相关的信 ...

  3. Cookie获取、设置值

    设置: HttpCookie cookie = new HttpCookie("cookieName"); cookie.Value = "name1" Htt ...

  4. linux cmd: ps

    每天一个linux命令(41):ps命令 http://www.cnblogs.com/peida/archive/2012/12/19/2824418.html Linux中的ps命令是Proces ...

  5. Android百度地图的简单实现

    2015-06-13 最近学习了百度地图API的简单开发,现记录如下:(持续更新中) 百度地图API是为开发者免费提供的一套基于百度地图服务的应用接口,包括JavaScript API.Web服务AP ...

  6. 【转载】Qt中的QString,QByteArray,Qchar, char*

    先要说的是QString.         之所以把QString单独拿出来,是因为string是很常用的一个数据结构,甚至在很多语言中,比如JavaScript,都是把string作为一种同int等 ...

  7. Immediate Decodability问题Java解答

    DescriptionAn encoding of a set of symbols is said to be immediately decodable if no code for one sy ...

  8. JavaEE学习之类加载器

    类装载子系统 在JAVA虚拟机中,负责查找并装载类型的那部分被称为类装载子系统. JAVA虚拟机有两种类装载器:启动类装载器和用户自定义类装载器.前者是JAVA虚拟机实现的一部分,后者则是Java程序 ...

  9. servlet操作数据库

    工具:myeclipse 数据库工具:mysql java ee操作数据库,首先要导入数据库驱动文件,我用的是mysql 刚开始,很多人代码正确但是就是连接不上,原因就是忘了驱动文件的导入. 我的驱动 ...

  10. JS 获取浏览器窗口大小clientWidth、offsetWidth、scrollWidth

    常用: JS 获取浏览器窗口大小   // 获取窗口宽度 if (windows.innerWidth) winWidth = windows.innerWidth; else if ((docume ...