1. 程式人生 > >尤拉計劃問題十七matlab實現

尤拉計劃問題十七matlab實現

Problem 17 :Number letter counts

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.

思路 :

分類計算:

  • 1到20字母拼寫無規律,直接算。
  • 接下來從20-100,數字字母組合為從20開始,並且是10的倍數的數後面加1到9的組合。
  • 從100 -1000,同理,只不過加了hundred, and,其他的與前面類似。

我的想法就是先寫一個翻譯函式,就是給你一個數字,然後你把數字翻譯成英語單詞,英語單詞是字串形式的,可以計算出每個英語單詞的字串長度,最後加和。

在此之前,我手算出了答案,很有規律的,很好算!

程式碼 :

將數字翻譯成英語函式的程式碼:

function t = num2words(i)
%i = input('');
if i > 0 && i < 20
    A = [{'one'},{'two'},{'three'},{'four'},{'five'},{'six'},{'seven'},{'eight'},{'nine'},{'ten'},{'eleven'},{'twelve'},{'thirteen'},{'fourteen'},{'fifteen'},{'sixteen'},{'seventeen'},{'eighteen'},{'nineteen'}];
    t = char(A(i));    
%     disp(t);   
end
if i >= 20 && i <= 100
    A = [{'one'},{'two'},{'three'},{'four'},{'five'},{'six'},{'seven'},{'eight'},{'nine'},{'ten'},{'eleven'},{'twelve'},{'thirteen'},{'fourteen'},{'fifteen'},{'sixteen'},{'seventeen'},{'eighteen'},{'nineteen'}];
    B = [{'ten'},{'twenty'},{'thirty'},{'forty'},{'fifty'},{'sixty'},{'seventy'},{'eighty'},{'ninety'},{'hundred'}];
    k = floor(i/10);   %first
    k1 = mod(i,10);      %second
    if k1 == 0
        k1 = floor(i/10);
        t = char(B(k1));
        return
    else
        t2 = char(B(k));%
        t3 = char(A(k1));
        c = 9;
        t = strcat(t2,c,t3);
%     disp(t4);
    end
end
if i > 100 && i <= 1000
   A = [{'one'},{'two'},{'three'},{'four'},{'five'},{'six'},{'seven'},{'eight'},{'nine'},{'ten'},{'eleven'},{'twelve'},{'thirteen'},{'fourteen'},{'fifteen'},{'sixteen'},{'seventeen'},{'eighteen'},{'nineteen'}];
   B = [{'ten'},{'twenty'},{'thirty'},{'forty'},{'fifty'},{'sixty'},{'seventy'},{'eighty'},{'ninety'},{'hundred'},{'thousand'}];
   k1 = floor(i/100); %first
   if k1 == 10
       disp(B(11));
       return
   end
   k2 = floor((i-k1*100)/10);
   if k2 == 0
       k3 = mod((i-k1*100),10);
       t2 = char(A(k1));
       t3 = char(B(10));
       t4 = 'and';
       t5 = char(A(k3));
       c = 9;
       t = strcat(t2,c,t3,c,t4,c,t5);
%        disp(t6);
   elseif k2 == 1
       k3 = mod((i-k1*100),100);
       t2 = char(A(k1));%first
       t3 = char(B(10));%second
       t4 = 'and';
       t5 = char(A(k3));
       c = 9;
       t = strcat(t2,c,t3,c,t4,c,t5);
%        disp(t);
   else
       k3 = mod((i-k1*100),10);
       t2 = char(A(k1));%first
       t3 = char(B(10));%second
       t4 = 'and';
       if k3 == 0
          k3 = floor((i-k1*100)/10);
          t5 = char(B(k3));
          c = 9;
          t = strcat(t2,c,t3,c,t4,c,t5);
          return
       else
           t5 = char(B(k2));
           t6 = char(A(k3));
           c = 9;
           t = strcat(t2,c,t3,c,t4,c,t5,c,t6);    
%        disp(t);
       end
   end
end

呼叫方式很簡單,舉個例子:

計算字串長度的的程式碼:

s = 0;
for i = 1:1000
    s(i) = length(num2words(i));
    s = s + s(i);
end
s

這個程式碼沒跑出來,有報錯,哪位大神能幫忙看看? 

 結果 :21124(手算的)

小結 :

          翻譯的時候那個格式不是很對,應該是字串連線方式的問題,這就是解題的關鍵了,等我再研究研究,等我弄出來在更新嘍!大家幫忙看看,有什麼地方需要改改,共同交流一下!