zoukankan      html  css  js  c++  java
  • DP:Humble Numbers,丑数

    描述A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers. 

    Write a program to find and print the nth element in this sequence.
    输入The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.输出For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output.样例输入
    1
    2
    3
    4
    11
    12
    13
    21
    22
    23
    100
    1000
    5842
    0
    
    样例输出
    The 1st humble number is 1.
    The 2nd humble number is 2.
    The 3rd humble number is 3.
    The 4th humble number is 4.
    The 11th humble number is 12.
    The 12th humble number is 14.
    The 13th humble number is 15.
    The 21st humble number is 28.
    The 22nd humble number is 30.
    The 23rd humble number is 32.
    The 100th humble number is 450.
    The 1000th humble number is 385875.
    The 5842nd humble number is 2000000000.

    思路:

    第i个丑数能通过之前的某个丑数求得
    所以第一个思路:求第i个丑数时,枚举1到i-1个丑数,求得num[j]*(2/3/5/7)(1<=j<=i-1,且这个数>num[i-1])的最小值
    这个思路是n^2的,很明显会超时。
    那么如何快速的通过前i-1个丑数更新第i个呢
    由此想到通过不断更新的方法。
    a表示第a个丑数*2后可能成为当前要求的第i个丑数
    b~~~~~b~~~~~~3~~~~~~~~~~~~~~~~~~~~~~~~~
    c~~~~~c~~~~~~5~~~~~~~~~~~~~~~~~~~~~~~~~
    d~~~~~d~~~~~~7~~~~~~~~~~~~~~~~~~~~~~~~~

    f[1]=1
    a=1 b=1 c=1 d=1
    f[i]=min(f[a]*2,f[b]*3,f[c]*5,f[d]*7)
    如果当前由a求得了f[i],那么inc(a);
    如果~~~~~b~~~~~~~~~~~~~~~~~~b
    ~~~~~~~~~c~~~~~~~~~~~~~~~~~~c
    ~~~~~~~~~d~~~~~~~~~~~~~~~~~~d

    不懂英语,不足以行走江湖。
    题中输出序数词是一个坑点。
    1:st 1001:st
    2:nd 1002:nd
    3:rd 1003:rd
    11:th 1011:th
    12:th 1012:th
    13:th 1013:th



    code:
    var f:array[0..5842]of int64;
    n,i,j,k:longint;
    a,b,c,d:longint;
    min,minj:longint;
    init:array[0..5842]of longint;
    mm:longint;
    begin f[1]:=1;   f[0]:=0;
    a:=1; b:=1;
    c:=1; d:=1;
    for i:=2 to 5842 do
    begin min:=maxlongint;
    if (f[a]*2<min)and(f[a]*2>f[i-1])
    then min:=f[a]*2;
    if (f[b]*3<min)and(f[b]*3>f[i-1])
    then min:=f[b]*3;
    if (f[c]*5<min)and(f[c]*5>f[i-1])
    then min:=f[c]*5;
    if (f[d]*7<min)and(f[d]*7>f[i-1])
    then min:=f[d]*7;
    f[i]:=min;
    if f[a]*2=min then inc(a);
    if f[b]*3=min then inc(b);
    if f[c]*5=min then inc(c);
    if f[d]*7=min then inc(d);
    end;
    readln(n);
    while n<>0 do
    begin write('The ',n);
    if (n mod 10=1)and(n mod 100<>11) then write('st')
    else if (n mod 10=2)and(n mod 100<>12) then write('nd')
    else if (n mod 10=3)and(n mod 100<>13) then write('rd')
    else write('th');
                      writeln(' humble number is ',f[n],'.');
    readln(n);
    end;
    end.
  • 相关阅读:
    世界黑客怎么排名?曝郭盛华公司30万美元收购海外域名,怎么回事
    AI应该享有与动物一样的权利吗?
    2020年将会迎来人工智能新浪潮,哪些商业巨头已经提前布局好了?
    揭秘郭盛华的真实收入,事实和你想的真不一样
    Excel表格中单击一个单元格如何将整行整列变色
    ldconfig与 /etc/ld.so.conf
    在excel中,应用公式到多行
    Excel怎么把两个单元格中的文字合并到一个单元格中
    在EXCEL中批量添加超链接
    windows中对文件进行排序
  • 原文地址:https://www.cnblogs.com/spiderKK/p/4919196.html
Copyright © 2011-2022 走看看