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. 什么是epoll

    什么是epoll epoll是什么?按照man手册的说法:是为处理大批量句柄而作了改进的poll.当然,这不是2.6内核才有的,它是在2.5.44内核中被引进的(epoll(4) is a new A ...

  2. HDU 5763 Another Meaning(FFT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5763 [题目大意] 给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式. [题解 ...

  3. HDU 3613 Best Reward(扩展KMP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3613 [题目大意] 一个字符串的价值定义为,当它是一个回文串的时候,价值为每个字符的价值的和,如果 ...

  4. Struts 和Spring的核心控制器

    Struts 核心控制器是FilterDispatch Spring核心控制器是DispatchServlet

  5. 打开固定文件的pr_debug

    驱动中pr_debug定义在kernel/include/linux/printk.h /* If you are writing a driver, please usedev_dbg instea ...

  6. ANDROID对文件的操作

    import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Inp ...

  7. HTTP学习笔记——URL与资源

    什么是URL? 所有的东西都有一个标准化的东西,公交有线路号,飞机有航班号,个人有身份证号,你坐出租车,告诉司机师傅我要到石牌华师,他就能明白你的意思了.URL就是因特网资源的标准化名称.URL指向一 ...

  8. 新建一个类并绑定一个activity

    1.新建一个类(.java 文件),继承Android.app.Activity 2.新建一个activity 文件 3.重写onCreate 方法,设置绑定activity 文件 @Override ...

  9. button 变成圆

      btn.layer.cornerRdius = width/2.0;btn.layer.maskToBounds = width/2.0:   

  10. ArrayList-VS-LinkedList

    ArrayList 是List接口的实现类:底层的数据结构是数组,而LinkedList底层数据结构是双向循环链表. 所以在查询时ArrayList效率高,增删时LinkedList高.由于List中 ...