zoukankan      html  css  js  c++  java
  • 丑数

    问题 B: 丑数

    时间限制: 1 Sec  内存限制: 32 MB
    提交: 1950  解决: 492
    [提交][状态][讨论版]

    题目描述

    如果一个数的素因子只包含2,3,5或7,那么我们把这种数叫做丑数。序列1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,25,27...展示了前20个丑数。
    请你编程寻找这个序列中的第n个元素。

    输入

    输入包含多组测试数据。每组输入为一个整数n(1<=n<=5842),当n=0时,输入结束。

    输出

    对于每组输入,输出一行“The nth humble number is number.”。里面的n由输入中的n值替换,“st”,“nd”,“rd”和“th”这些序数结尾的用法参照输出样例。

    样例输入

    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.
    思路:
    丑数的2.3.5.7倍数也是丑数;
    就从2.3.5.7开始枚举其2.3.5.7倍,讲最小的数也标记为丑数,
    以此类推找到相应多的丑数
    #include<iostream>
    using namespace std;
    int a[5844];//存丑数的数组 
    int main(){
        a[1]=1;
        int t2=1;
        int t3=1;
        int t5=1;
        int t7=1;
        int minn,t;
        for(int i=2;i<=5843;i++){
            minn=min(a[t2]*2,min(a[t3]*3,min(a[t5]*5,a[t7]*7)));
            a[i]=minn;//一个个记录丑数 
            if(a[t2]*2==minn) t2++;
            if(a[t3]*3==minn) t3++;
            if(a[t5]*5==minn) t5++;
            if(a[t7]*7==minn) t7++;
        }
        while(cin>>t){
            if(t==0) return 0;
            else
            cout<<"The "<<t;
            if(t%10==1&&t%100!=11) cout<<"st";
            else if(t%10==2&&t%100!=12) cout<<"nd";
            else if(t%10==3&&t%100!=13) cout<<"rd";
            else cout<<"th";
            cout<<" humble number is "<<a[t]<<"."<<endl;
        }
        return 0;
    } 
     
  • 相关阅读:
    spring mybatis 关于 basepackage 和 mapperLocations 的通配符匹配实例
    nginx 隐藏版本信息
    nginx 虚拟主机配置
    分析解决 spring quartz 中出现的执行两次问题
    nginx 安装配置和常用命令
    JMX 远程监控 Linux tomcat 功能实现
    jvm 类加载机制
    jvm 类文件结构学习
    转 MySQL: Starting MySQL….. ERROR! The server quit without updating PID file解决办法
    MySQL提示:The server quit without updating PID file问题的解决办法
  • 原文地址:https://www.cnblogs.com/yfr2zaz/p/11082621.html
Copyright © 2011-2022 走看看