18005 It is not ugly number

该博客主要介绍了一个编程挑战,即如何找到第n个非丑数。丑数是指仅包含质因数2、3或5的正整数,而非丑数则是指不满足这一条件的数。博客提供了一段C++代码,用于找出给定n后的第n个非丑数。代码首先定义了一个判断是否为丑数的函数`ugly()`,然后通过遍历并存储所有丑数来查找非丑数。在输入样例中,程序成功地计算了第1、2和9个非丑数,分别输出了7、11和23。
摘要由CSDN通过智能技术生成

时间限制:2000MS 代码长度限制:10KB
提交次数:0 通过次数:0

题型: 编程题 语言: G++;GCC
Description
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, 10, 12, …shows the
first 10 ugly numbers. By convention, 1 is included. Then, here are the first 10 Not ugly numbers:7, 11, 13, 14, 17, 19,
21, 22, 23, 26. Given the integer n, write a program to find and print the n’th Not ugly number.

输入格式
First line is T(T<=10000), the number of cases.
The T lines following. Each line of the input contains a positive integer n (n <= 100000000).

输出格式
For each case, output the n’th Not ugly number .

输入样例
3
1
2
9

输出样例
7
11
23

#include<iostream>
#include<stdio.h>
int a[1200];
using namespace std;
int ugly(int n)
{
	while (n % 2 == 0)
		n = n / 2;
	while (n % 3 == 0)
		n = n / 3;
	while (n % 5 == 0)
		n = n / 5;
	if (n == 1)
		return 1;
	else
		return 0;
}
int main()
{
	int i;
	int x = 0;
	for (i = 1; i <= 100000000; i++)
	{
		if (ugly(i))
		{
			a[x] = i;
			x++;
		}
	}
	int n;
	cin >> n;
	int count = 0;
	while (n--)
	{
		int m;
		cin >> m;
		for (i = 0; i <= 1200; i++)
		{
			count += a[i + 1] - a[i] - 1;
			if (count >= m)
				break;
		}
		cout << a[i + 1] + m - count - 1 << endl;
		count = 0;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值