Humble Numbers 丑数

2024-08-28 07:48
文章标签 numbers 丑数 humble

本文主要是介绍Humble Numbers 丑数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://acm.hdu.edu.cn/showproblem.php?pid=1058
很多人都说这道题是水题,但对于我来说还是很有用的,它教会我在处理这种问题的时候为了节省时间我们可以先把所有的满足范围的丑数找出来,再进行下面的操作。和去年吉林省省赛那道素数题相似。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657

const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
return a>b;
}
int a[6000];
void init()
{
int n1=1,n2=1,n3=1,n4=1;
int k=1;a[1]=1;
while(k<=5842)
{
k++;
a[k]=min(a[n1]*2,min(a[n2]*3,min(a[n3]*5,a[n4]*7)));
if(a[k]==a[n1]*2)n1++;
if(a[k]==a[n2]*3)n2++;
if(a[k]==a[n3]*5)n3++;
if(a[k]==a[n4]*7)n4++;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
init();
int n;
while(cin>>n&&n)
{
if(n==0)break;
if(n%10==1&&n%100!=11)cout<<"The "<<n<<"st humble number is "<<a[n]<<"."<<endl;
else if(n%10==2&&n%100!=12)cout<<"The "<<n<<"nd humble number is "<<a[n]<<"."<<endl;
else if(n%10==3&&n%100!=13)cout<<"The "<<n<<"rd humble number is "<<a[n]<<"."<<endl;
else cout<<"The "<<n<<"th humble number is "<<a[n]<<"."<<endl;

}
return 0;
}


这篇关于Humble Numbers 丑数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1114159

相关文章

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

高精度打表-Factoring Large Numbers

求斐波那契数,不打表的话会超时,打表的话普通的高精度开不出来那么大的数组,不如一个int存8位,特殊处理一下,具体看代码 #include<stdio.h>#include<string.h>#define MAX_SIZE 5005#define LEN 150#define to 100000000/*一个int存8位*/int num[MAX_SIZE][LEN];void

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

【codeforces】55D. Beautiful numbers 数位DP

传送门:【codeforces】55D. Beautiful numbers 题目分析:被每一位整除则用二进制记录已经包括的数字的个数,以及对2520取模后的状态。由于对5整除当且仅当最后一个数为0或5,对2整除当且仅当最后一个数为偶数,且1~9的最小公倍数为2520,不包括2,5后的最小公倍数为252,所以除最后一层对2520取模,其余时候都对252取模即可。由于整除的状态有限,最多只有

【SGU】113. Nearly prime numbers 合数分解

传送门:【SGU】113. Nearly prime numbers 题目分析:O(sqrt(N))。。 代码如下: #include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std ;#define rep( i , a , b ) for

USACO Section 3.1 Humble Numbers

题意: 已知一个集合S  由S的任意子集作为因子  可构造出一个数字  求  这些构造出的数字中第k大的数字是多少 思路: 拿到这题就被“数字不是很多而且比较连续暴力枚举就好”这个思路迷惑了  果断TLE… 跪了一次后想到通过bfs构造可取  这时用了queue维护bfs  用priority_queue维护答案(大顶堆  内部最多k个数字)  用set判重复(5*2=2*5)  写

Sum of Square Numbers

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. Example 1: Input: 5Output: TrueExplanation: 1 * 1 + 2 * 2 = 5 Example 2: Inpu

JavaScript - First step - Numbers and operators

Types of numbers Integers 整数Floating point numbers 单精度浮点数Doubles 双精度浮点数Binary 二进制Octal 八进制Hexadecimal 十六进制 Arithmetic operators 算术运算符 + 加法- 减法* 乘法/ 除法% 求余** 指数 (次方 5 ** 5 = 5 * 5 * 5 * 5 * 5) Oper

leetcode 263:丑数

bool isUgly(int num) {if(num<=0)return false;std::vector<int> a;if(num<7)return true;while(num!=1){if(num%5==0){num=num/5;}else if(num%3==0){num=num/3;}else if(num%2==0){num=num/2;}elsereturn false;