(Problem 22)Names scores
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的更多相关文章
- (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, ...
- (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 ...
- (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 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 ...
- 基于Web的企业网和互联网的信息和应用( 1194.22 )
基于Web的企业网和互联网的信息和应用( 1194.22 ) 原文更新日期: 2001年6月21日原文地址: http://www.access-board.gov/sec508/guide/1194 ...
- (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 ...
- (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 ...
- (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 ...
- (Problem 70)Totient permutation
Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...
随机推荐
- oracle slient静默安装并配置数据库及仅安装数据库不配置数据库shell
<1,>仅安装数据库软件不配置数据库 ./x86oracle.sh /ruiy/ocr/DBSoftware/app/oracle /ruiy/ocr/DBSoftware/app/ora ...
- 30天自制操作系统第八天学习笔记(u盘软盘双启动版本)
暑假学习小日本的那本书:30天自制操作系统 qq交流群:122358078 ,更多学习中的问题.资料,群里分享 environment:开发环境:ubuntu 第八天的学习思考: 关于鼠标是怎么 ...
- 行内元素有哪些?块级元素有哪些?CSS的盒模型?转载
块级元素:div p h1 h2 h3 h4 form ul行内元素: a b br i span input selectCss盒模型:内容,border ,margin,padding css中的 ...
- 全面理解js面向对象
前言 当今 JavaScript 大行其道,各种应用对其依赖日深.web 程序员已逐渐习惯使用各种优秀的 JavaScript 框架快速开发 Web 应用,从而忽略了对原生 JavaScript 的学 ...
- 搭建Myeclipse下Java Web开发环境
1.准备 先下载软件:Myeclipse:http://www.xiazaiba.com/html/23858.html tomcat:http://files.cnblogs.com/files/l ...
- C++对象模型--C++对象模型
何为C++对象模型? 部分: 1 语言中直接支持面向对象程序设计的部分 2 对于各种支持的底层实现机制 语言中直接支持面向对象程序设计的部分,如构造函数.析构函数.虚函数.继承 ...
- java的IO流包装不当导致从网页获取的数据出现乱码
从网页上获取数据时必须要注意字符集的问题.处理不慎确实苦不堪言. 例如通过URL连接时,将字节流InputStream包装成字符流(以便直接存为String)时,一定要注意加上charsetName这 ...
- JBoss 系列二十一:JBossCache核心API
内容简介 本处介绍JBossCache相关的主要API,我们目的通过本部分描述,读者可以使用JBossCache API,在自己的应用中使用JBossCache. Cache接口 Cache 接口是和 ...
- 5.6.3.4 trim()方法
ECMAScript 5 为所有字符串定义了trim()方法.这个方法会创建一个字符串的副本,删除前置以及后缀的所有空格,然后返回结果.例如: var stringValue = " hel ...
- 视频(其他)下载+tomcat 配置编码+图片上传限制大小
视频下载:前台 jsp function downVideo(value,row,index){ return '<a href="<%=basePath%>admin/v ...