Solution to Project 22 on Project Euler

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?

My solution:

``````#include <stdio.h>

#define MAX 5163

int scmp(int i,int j,char lista[MAX][16]){
if (lista[i][0]>lista[j][0])
return 1;
else if (lista[i][0]<lista[j][0])
return 0;
else {
if (lista[i][1]>lista[j][1])
return 1;
else if (lista[i][1]<lista[j][1])
return 0;
else {
if (lista[i][2]>lista[j][2])
return 1;
else if (lista[i][2]<lista[j][2])
return 0;
else {
if (lista[i][3]>lista[j][3])
return 1;
else if (lista[i][3]<lista[j][3])
return 0;
else {
if (lista[i][4]>lista[j][4])
return 1;
else if (lista[i][4]<lista[j][4])
return 0;
else {
if (lista[i][5]>lista[j][5])
return 1;
else if (lista[i][5]<lista[j][5])
return 0;
else {
if (lista[i][6]>lista[j][6])
return 1;
else if (lista[i][6]<lista[j][6])
return 0;
else {
if (lista[i][7]>lista[j][7])
return 1;
else if (lista[i][7]<lista[j][7])
return 0;
else {
if (lista[i][8]>lista[j][8])
return 1;
else if (lista[i][8]<lista[j][8])
return 0;
else {
if (lista[i][9]>lista[j][9])
return 1;
else if (lista[i][9]<lista[j][9])
return 0;
else
return 0;
}

}

}
}
}
}
}
}
}
}

int main(){
int c,i,j,sum,partial,x,y,z,n,inicio;
char lista[MAX][16];
char temp[16];

for (i=0;i<MAX;i++)
for (j=0;j<16;j++)
lista[i][j]='@';

i=0;
j=0;

while ((c=getchar()) != EOF){
if (c=='"')
continue;
if (c==','){
i++;
j=0;
continue;
}
lista[i][j]=c;
j++;
}

inicio=1;
for (i=0;i<MAX;i++){
for (j=inicio;j<MAX;j++)
if (scmp(i,j,lista)){
for (n=0;n<16;n++)
temp[n]=lista[i][n];
for (n=0;n<16;n++)
lista[i][n]=lista[j][n];
for (n=0;n<16;n++)
lista[j][n]=temp[n];
}
inicio++;
}

for (i=0;i<MAX;i++){
for (j=0;j<16;j++)
printf("%c",lista[i][j]);
printf("n");
}

sum=0;
for (i=0;i<MAX;i++){
partial=0;
for (j=0;j<16;j++)
partial+=(lista[i][j]-64);
partial*=(i+1);
sum+=partial;
}
printf("sum = %dn",sum);
return 0;
}``````