24小时热门版块排行榜    

CyRhmU.jpeg
南方科技大学公共卫生及应急管理学院2026级博士研究生招生报考通知(长期有效)
查看: 1526  |  回复: 13
本帖产生 2 个 程序强帖 ,点击这里进行查看
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

holmescn

金虫 (正式写手)

[交流] Euler 工程 第廿二题: 姓的总分已有5人参与

附件中是一个包含了5前个姓的文件。先把它按字母表排序,然后计算每个姓的值,并乘以这个姓在文件中的序数得到这个姓的分数。
例如,COLIN,它的字母值是:3+15+12+9+14=53,在排序后的列表中,它在第938位,这样COLIN的得分为:53*938=49714
那么这个文件中所有姓的总分是多少?
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

qinghuoly

木虫 (正式写手)

★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
jjdg(金币+2): 感谢参与 2011-06-27 03:02:22
jjdg(程序强帖+1): 辛苦了 2011-06-27 03:02:39
scheme解法一:

[define [ans22]
  [define l [read-line [open-input-file "22.txt"]]]                ;读取文件
  
  [define namelist
    [with-input-from-string
     [regexp-replace* "," l " "]                ;替换逗号为空格
     [lambda [] [let loop [[l '[]] [r [read]]]
                  [if [eof-object? r] l
                      [loop [cons r l][read]]]]]]]                ;读取为列表
  
  [define [conv str]                ;将字符串转化为数值和
    [apply +
           [map [lambda [c]
                  [- [char->integer c]
                     64]]
                [string->list str]]]]
  
  [define [ex ls sum n ]
    [if [null? ls]
        sum
        [ex [cdr ls]
            [+ sum
               [* n
                  [conv [car ls]]]]
            [add1 n]]]]
  
  [ex [sort namelist string
        > [time [ans22]]
        cpu time: 1531 real time: 1531 gc time: 188
        871198282

scheme解法二:
[define names-list  '[
                      "MARY"
                      "PATRICIA"
                      "LINDA"
                      "BARBARA"
    .......
                      "ALONSO" ]]

[define [conv str]
  [apply +
         [map [lambda [c]
              [- [char->integer c]
                 64]]
              [string->list str]]]]

[define [ex ls sum n ]
      [if [null? ls]
      sum
      [ex [cdr ls]
          [+ sum
             [* n
                [conv [car ls]]]]
          [add1 n]]]]

[ex [sort names-list string
;define the data in source code will be faster.

        > [time [ans22]]
        cpu time: 47 real time: 47 gc time: 0
        871198282
天地为帐,日月为灯,风雷为号角,云虹为旗令,山川为阵图,草木为兵卒。运阴阳五行为谋,策古今兴替为略。
12楼2011-06-26 01:42:04
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 14 个回答

libralibra

至尊木虫 (著名写手)

骠骑将军

★ ★ ★ ★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
dubo(金币+1): 谢谢交流 2011-06-05 12:17:18
微尘、梦想(金币+4): 2011-06-06 20:19:45
matlab code
CODE:
%% What is the total of all the name scores in the file?
% 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.
function result = euler22()
tic;
fid = fopen('D:\euler\names.txt');
names = fgets(fid); % 读取全部内容到字符串
fclose(fid);

names = strrep(names,'"',''); % 删除"
namelist = regexp(names,',','split'); % 用逗号分隔
sname = sort(namelist); % 排序

result = 0;
for i=1:length(sname) % 循环
    curname = lower(sname{i}); % 全小写
    curname = curname-repmat('a',1,length(curname)); % 与全a作差
    cursum = sum(curname+1); % 求和
    result = result+cursum*i; % 乘顺序计算score,累加
end
toc;
end

结果时间
CODE:
% Elapsed time is 0.307340 seconds.
% ans =
%    871198282

matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
2楼2011-06-05 01:32:32
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

huycwork

金虫 (著名写手)

★ ★ ★ ★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
dubo(金币+1): 谢谢交流 2011-06-05 12:17:28
微尘、梦想(金币+4): 2011-06-06 20:19:54
涉及到文本处理的惯例是Perl:
CODE:
#!/usr/bin/perl
open F, "<", "names.txt";
$f = ;
@ns = sort eval ($f);
foreach(@ns){
    local (*v) = \$_;
    $v += ord($_) - ord('A') + 1 foreach(/(.)/g);
}
unshift @ns, 0;
$s += $_ * $ns[$_] foreach(1..@ns);
print $s;

[ Last edited by huycwork on 2011-6-6 at 09:27 ]
漩涡的中心有一块空地,空空的。
3楼2011-06-05 10:12:44
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

wangww2011

木虫 (著名写手)

★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
xzhdty(金币+2): 谢谢交流 2011-06-05 14:21:41
余泽成(程序强帖+1): 鼓励交流! 2011-06-18 15:55:16
话说perl版的真是简洁
还是用C写吧,虽然看着挺不爽的
CODE:
#include
#include
#include


#define SIZE 50000

inline int cmp(const void *p1,const void *p2)
{
        return strcmp((char *)p1,(char *)p2);
}

inline int count(const char *p){
        int i=0,res=0;
        while(p[i]!='\0')res+=p[i++]-64;
        return res;
}

long euler22(){
        int i=0,length;
        FILE *fp=fopen("names.txt", "r");
        if(fp == 0) return -1;

        char str[SIZE];
        if (NULL == fgets(str, SIZE, fp)) {
                return -1;
        }
        fclose(fp);

        char *delims="\",";
        char *p=strtok(str,delims);
        char names[6000][15];
        while(p!=NULL){
                strcpy(names[i++],p);
                p=strtok(NULL,delims);
        }
        length=i;
  
        qsort(names,length,sizeof(names[0]),cmp);
  
        long  sum=0;
        for(i=0;i                 sum+=(i+1)*count(names[i]);
      
        return sum;
}



int main(void){

        printf("%ld\n",euler22());
  
        return 0;
}

4楼2011-06-05 13:42:07
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通表情 高级回复(可上传附件)
信息提示
请填处理意见