zoukankan      html  css  js  c++  java
  • 专题三--1004

    题目

    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.
    
    

    题目大意

    humble number是因子有且仅有2,3,5,7四个中至少一个的数(包括1).求第n个humble number。

    解题思路

    本着将问题分解为子问题的思路,可以将每个humble number看作2,3,5,7的相乘组合。即(n为humble number数):

    n=(2^a)(3^b)(5^c)*(7^d)

    确定了a,b,c,d的值即确定了n,先确定好前5842个humble number,存在数组中,直接输出第n个即可

    AC代码

    #include<iostream>
    #include<string>
    #include<stdio.h>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    long long s[6000] = { 0 };
    #define MAX  2000000000
    void jisuan();
    int main(){
        jisuan();
        int n;
        while (cin >> n&&n != 0){
            if (n % 10 == 1 && n % 100 != 11)
                cout << "The " << n << "st" << " humble number is " << s[n] << "." << endl;
            else if (n % 10 == 2 && n % 100 != 12)
            cout << "The " << n << "nd" << " humble number is " << s[n] << "." << endl;
            else if (n % 10 == 3 && n % 100 != 13)
                cout << "The " << n << "rd" << " humble number is " << s[n] << "." << endl;
            else cout << "The " << n << "th" << " humble number is " << s[n] << "." << endl;
            //cout<<"liu"<<endl;
        }
    }
    void jisuan(){
        int a, b, c, d;
        int len = 1;
        long long num2, num3, num5, num7;
        for (a = 0; a <= 31; a++)
        {
            num2 = (long long)pow(2.0, (double)a);
            for (b = 0; b <= 20; b++)
            {
                num3 = (long long)pow(3.0, (double)b);
                if (num2*num3 > MAX)
                    break;
                for (c = 0; c <= 14; c++)
                {
                    num5 = (long long)pow(5.0, (double)c);
                    if (num2*num3*num5 > MAX)
                        break;
                    for (d = 0; d <= 12; d++)
                    {
                        num7 = (long long)pow(7.0, (double)d);
                        if (num2*num3*num5*num7 <= MAX)
                            s[len++] = num2*num3*num5*num7;
                        else break;
                    }
                }
            }
        }
        sort(s+1, s+ len);
    }
    
  • 相关阅读:
    PHP腾讯地图地图接口调用提示{ "status": 110, "message": "请求来源未被授权,此次请求无来源信息" }
    PHP带参数匿名函数
    微信小程序实现图片预加载(图片延迟加载)
    快速排序
    《Linux命令行与shell脚本编程大全》第十八章 图形化桌面环境中的脚本编程
    《Linux命令行与shell脚本编程大全》第十七章 创建函数
    makefile中伪目标的理解
    《Linux命令行与shell脚本编程大全》第十六章 控制脚本
    《Linux命令行与shell脚本编程大全》第十五章 呈现数据
    《Linux命令行与shell脚本编程大全》第十四章 处理用户输入
  • 原文地址:https://www.cnblogs.com/liuzhanshan/p/5521625.html
Copyright © 2011-2022 走看看