工学1号馆

home

Project Euler 22–Names scores

Wu Yudong    August 12, 2018     欧拉计划   837

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?

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

struct str{
char name[12];
};

char a[12];

int namevalue(char *s)
{
int i, sum;
i = sum = 0;
while(s[i]) {
sum += (s[i] - 'A' + 1);
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 = 0;

struct str namestring[5163];

fp = fopen("names.txt", "r");
fseek(fp, 0, SEEK_END);
int file_size;
file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
s = (char*)malloc(file_size * sizeof(char));

i = j = k = 0;
while(i <= file_size) {
c = s[i++];
if(!isalpha(c)) {
if(c == ',') {
j = 0;
strcpy(namestring[k++].name, a);
memset(a,'\0',12 * sizeof(char));
}
} else {
a[j++] = c;
}
}
strcpy(namestring[k].name, a);
memset(a,'\0',12 * sizeof(char));

qsort(namestring, 5163, sizeof(namestring[0]),cmp);

for(i = 0; i <= 5162; i++) {
sum += (namevalue(namestring[i].name) * (i + 1));
}
printf("%lld\n",sum);
}

int main(void)
{
solve();
return 0;
}