返回列表 发帖
回复 1# codegay
ord就是快
该题的单词文件为
#ProjectEuler 42:Coded triangle numbers
#2016/10/15 by HAPPY
i=0
for Line in open('p042_words.txt','r'):
Line=Line.strip("\"").split("\",\"")
for W in Line:
S=0
for Char in W:
S+=ord(Char)-64
if( (8*S+1)**0.5==int((8*S+1)**0.5) and int((8*S+1)**0.5)&1==1 ):
i+=1
print(i)COPY

TOP

回复 3# codegay
那应该如何写,就是要取整,怎么写最好。

TOP

回复 5# 523066680
哦,我把C的写法带到python里了。
#include<stdio.h>
int main()
{
int i=0,sum,fsize;
char* Line;
FILE* fp=fopen("p042_words.txt", "rb");
if(fp==NULL){return 1;}
fseek(fp, 0, SEEK_END);
fsize=ftell(fp)+1;
Line=(char *)calloc(fsize, sizeof(char));
fseek(fp, 0, SEEK_SET);
fgets(Line, fsize, fp);
fclose(fp);
for(; *Line!='\0'; Line++){
if( 'A'<= *Line && *Line <='Z' ){
sum+=*Line-'A'+1;
}else{
if((int)sqrt(2*sum)*(int)(sqrt(2*sum)+1)-2*sum==0 && sum!=0){i++;}
sum=0;
}
}
printf("%d\n",i);
return 0;
}COPY

TOP

批处理版
@echo off&setlocal enabledelayedexpansion
(
for /l %%j in (1,1,16) do (
set/p str[%%j]=
set "str[%%j]=!str[%%j]:"=0!"
set "str[%%j]=!str[%%j]:,=0!"
)
)<p042_words.txt
for %%N in (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) do (set/a "i+=1,#%%N=i")
for /l %%j in (1,1,16) do (
set/a "p=%%j*100/16"&echo 正在计算...!p!%%
for /l %%i in (0,1,1080) do (
set "Char=!str[%%j]:~%%i,1!"
if not "!Char!"=="0" (
set/a "sum+=#!Char!"
) else (
for /l %%k in (1,1,20) do (
set/a "k=%%k*(%%k+1)/2"
if !k! equ !sum! (set/a "n+=1")
)
set/a "sum=0"
)
)
)
set/p=共有!n!个三角数单词COPY
1

评分人数

TOP

返回列表