24小时热门版块排行榜    

Znn3bq.jpeg
汕头大学海洋科学接受调剂
查看: 1783  |  回复: 13
本帖产生 2 个 程序强帖 ,点击这里进行查看
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

holmescn

金虫 (正式写手)

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

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

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

» 猜你喜欢

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

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

huycwork

金虫 (著名写手)

★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
微尘、梦想(金币+2): 鼓励多交流! 2011-06-06 20:20:23
维持最奇怪语言的地位还真是有压力啊,俺再来三行版:
CODE:
open F, "<", "names.txt";
$s+=++$b*$_ foreach(map{$a=0;$a+=$_ foreach(map{ord($_)-64}(/(.)/g));$a}sort eval);
print $s, "\n";

[ Last edited by huycwork on 2011-6-6 at 13:46 ]
漩涡的中心有一块空地,空空的。
8楼2011-06-06 13:45:03
已阅   回复此楼   关注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的回帖
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 22408 312求调剂 +16 门路摸摸 2026-04-14 17/850 2026-04-14 23:59 by Xurambo2014
[考研] 药学305求调剂 +6 玛卡巴卡boom 2026-04-11 6/300 2026-04-14 19:48 by zhouxiaoyu
[考研] 化学070300 求调剂 +23 哈哈哈^_^ 2026-04-12 23/1150 2026-04-14 16:30 by zhouxiaoyu
[考研] 药学305求调剂 +10 玛卡巴卡boom 2026-04-10 10/500 2026-04-14 15:55 by zs92450
[考研] 271求调剂 +35 2261744733 2026-04-11 41/2050 2026-04-14 15:36 by zs92450
[考研] 求调剂 +20 MAX怅惘 2026-04-09 22/1100 2026-04-14 14:57 by 独醉梦孤城
[考研] 300分求调剂 (085501机械专硕,本科扬大) +9 xu@841019 2026-04-11 10/500 2026-04-14 08:48 by 木木mumu~
[考研] 一志愿中南大学 0855 机械 286 求调剂 +11 不会吃肉 2026-04-12 11/550 2026-04-13 21:59 by bljnqdcc
[考研] +10 李多米lee. 2026-04-12 11/550 2026-04-12 22:58 by yuyin1233
[考研] 0831一轮调剂失败求助 +10 小熊睿睿_s 2026-04-11 10/500 2026-04-12 22:43 by 长弓傲
[考研] 0831生医工第一轮调剂失败求助 +12 小熊睿睿_s 2026-04-11 16/800 2026-04-12 16:28 by 钰璞
[考研] 一志愿华中农微生物,288分,三年实验经历 +11 代fish 2026-04-09 11/550 2026-04-12 10:21 by Hayaay
[考研] 药学专硕调剂 +8 ? 一路生?花? 2026-04-10 10/500 2026-04-11 21:21 by zhouxiaoyu
[考研] 283求调剂 +22 那个噜子 2026-04-09 22/1100 2026-04-11 10:41 by 逆水乘风
[考研] 085410-273求调剂 +6 X1999 2026-04-10 6/300 2026-04-11 10:32 by Delta2012
[考研] 293求调剂 +6 勇远库爱314 2026-04-08 6/300 2026-04-11 10:08 by zhq0425
[考研] 22408 327分求调剂 +4 韵风kon 2026-04-10 4/200 2026-04-11 09:51 by 猪会飞
[考研] 一志愿华东师范生物学326分,求调剂 +8 刘墨墨 2026-04-09 8/400 2026-04-10 12:00 by pengliang8036
[考研] 085501机械英二77总分294求调剂,接受跨专业学习 +6 守法公民亓纪 2026-04-08 6/300 2026-04-09 15:55 by wp06
[考研] 311求调剂 +6 surte 2026-04-08 13/650 2026-04-09 14:00 by surte
信息提示
请填处理意见