zoukankan      html  css  js  c++  java
  • (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[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));
        fread(s, sizeof(char), file_size, fp);
    
        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,'',12 * sizeof(char));
                }
            } else {
                a[j++] = c;
            }
        }
        strcpy(namestring[k].name, a);
        memset(a,'',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
    ",sum);
    }
    
    int main(void)
    {
        solve();    
        return 0;
    }
    Answer:
    871198282
  • 相关阅读:
    四瓶化学试剂合成了人工生命
    DNA 就是软件代码
    首例人造生命,碱基对达到100多万个
    看到一个新闻管理系统,看上去开源,其实是骗你使用
    日本可合成碱基对 遗传信息"无中生有"
    新生命如何在实验室“被创造”
    一个基因有多少碱基对
    A、T、G、C四个字母(构成碱基的基本单位)的不同排列,画出一张基因图谱
    远程图片下载不能显示的问题
    30亿个碱基对组成,分布在细胞核的23对染色体中
  • 原文地址:https://www.cnblogs.com/acutus/p/3546057.html
Copyright © 2011-2022 走看看