zoukankan      html  css  js  c++  java
  • HDOJ1058 Humble Numbers

    Humble Numbers

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 12573    Accepted Submission(s): 5509


    Problem Description
    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
     
    Input
    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.
     
    Output
    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.
     
    Sample Input
    1 2 3 4 11 12 13 21 22 23 100 1000 5842 0
     
    Sample Output
    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.
     
    Source
     
    Recommend
    JGShining
     

    分析:
    只能含有素数因子2或3或5或7的数为Humble Numbers,
    则可知:Humble Numbers的2倍或者3倍或者5倍或者7倍仍然是Humble Numbers,所以依次在当前找到的Humble Numbers中
    依次*2;*3;*5;*7依次找最小的向后递推。

    网上说这是经典的DP-----状态转移方程:
     ans(k) = min(ans(m)*2, ans(n)*3, ansF(p)*5, ans(q)*7)  (k > m,n,p,q)
                                                                                      特别的: m,n,p,q 只有在本项被选中后才移动
    我感觉这题就是利用数组的滚动递推推出来的。。。。。

     1 #include <cstdio>
     2 #include <iostream>
     3 
     4 using namespace std;
     5 
     6 int ans[5900];
     7 
     8 int min(int a, int b)
     9 {
    10     return a < b ? a : b;
    11 }
    12 
    13 void init()
    14 {
    15     ans[1] = 1;
    16     int m, n, p, q;
    17     m = n = p = q = 1;
    18     for(int i = 2; i <= 5842; ++i)  // 题目要求只能含有素数因子2或3或5或7,所以只能用Humble Numbers为基数去*2;*3;*5或*7
    19     {
    20         ans[i] = min(ans[m]*2, min(ans[n]*3, min(ans[p]*5, ans[q]*7)));
    21         if(ans[i] == ans[m] * 2)    // 注意四个 if 是并列的,可能存在重叠的情况,此时都要相应的增加,否则结果会重复
    22             ++m;
    23         if(ans[i] == ans[n] * 3)
    24             ++n;
    25         if(ans[i] == ans[p] * 5)
    26             ++p;
    27         if(ans[i] == ans[q] * 7)
    28             ++q;
    29     }
    30 }
    31 
    32 void output(int n)   // 这题打印结果要用到英文,很恶心人!
    33 {
    34     printf("The %d", n);
    35     int last = n%100;
    36     if(last==13 || last==12 || last==11)
    37     {
    38         printf("th humble number is %d.\n", ans[n]);
    39         return ;
    40     }
    41     last = n%10;
    42     if(last == 1)
    43         printf("st");
    44     else if(last == 2)
    45         printf("nd");
    46     else if(last == 3)
    47         printf("rd");
    48     else
    49         printf("th");
    50     printf(" humble number is %d.\n", ans[n]);
    51 }
    52 
    53 
    54 
    55 int main()
    56 {
    57     int n;
    58     init();
    59     while(scanf("%d", &n), n)
    60     {
    61         output(n);
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    POJ1258——Prim——Agri-Net
    POJ2485——Prim——Highways
    POJ1789——Prim——Truck History
    POJ1125——Floyed——Stockbroker Grapevine
    POJ2253——Floyed——Frogger
    字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String
    模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie
    DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
    queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards
    贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
  • 原文地址:https://www.cnblogs.com/dongsheng/p/3114297.html
Copyright © 2011-2022 走看看