24小时热门版块排行榜    

CyRhmU.jpeg
查看: 1519  |  回复: 13
本帖产生 2 个 程序强帖 ,点击这里进行查看
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

holmescn

金虫 (正式写手)

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

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

» 本帖已获得的红花(最新10朵)

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

wangww2011

木虫 (著名写手)

★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
微尘、梦想(金币+2): 鼓励多交流! 2011-06-06 20:20:38
ben_ladeng: 2011-08-02 08:08:03
引用回帖:
Originally posted by huycwork at 2011-06-06 13:45:03:
维持最奇怪语言的地位还真是有压力啊,俺再来三行版:

Perl还是很方便的,有机会学学
CODE:
print sum([(i+1)*sum([ord(c)-64 for c in s]) for i,s in enumerate(sorted(open('names.txt').read()[1:-1].split('","')))])

额 我走极端了

[ Last edited by wangww2011 on 2011-6-6 at 17:34 ]
9楼2011-06-06 17:33:28
已阅   回复此楼   关注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的回帖
普通表情 高级回复(可上传附件)
信息提示
请填处理意见