【UJN-寒假练练练】新年快乐-3--枚举

【UJN-寒假练练练】新年快乐-3--枚举,第1张

【UJN-寒假练练练】新年快乐-3--枚举

//
R - Ugly Numbers

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...
shows the first 10 ugly numbers. By convention, 1 is included.
Given the integer n,write a program to find and print the n'th ugly number.
Input
Each line of the input contains a postisive integer n (n <= 1500).
Input is terminated by a line with n=0.
Output
For each line, output the n’th ugly number .:Don’t deal with the line with n=0.
Sample Input
1
2
9
0
Sample Output
1
2
10

//
题意:只含有质因数 2 3 5 的数为丑数 求第 n 个丑数
//
题解:
01 由 素数分解唯一定理 可知 丑数的质因子只能有 2 3 5 
02 要求丑数的位次 通过比较最小值 打表

// 判断是否为丑数
bool is_ugly( int n )
{
    if( n<=0 ) return false;
    while( n%2==0 ) n/=2;
    while( n%3==0 ) n/=3;
    while( n%5==0 ) n/=5;
    return n==1;            // 返回语句真假值
}

// 丑数打表
#include
using namespace std;

typedef unsigned long long ull;
const int MAXN=1e4+5;

int ans[MAXN];

int my_min( int a,int b,int c )
{
    int temp=a 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5713635.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-18

发表评论

登录后才能评论

评论列表(0条)

    保存