POJ1338 & POJ2545 & POJ2591 & POJ2247

POJ1338 2545 2591 2247都是一个类型的题目,所以放到一起来总结

POJ1338:Ugly Numbers

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21708   Accepted: 9708

Description

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的数,实际这些数就是2、3、5这些数互相乘,从大到小排好序的序列。

发现这种题也有一个固定的套路,也渐渐知道模板题是个什么概念了。

代码:

#include <iostream>
using namespace std;

int main()
{
	int a[1500]={1},i=1,j2=0,j3=0,j5=0,m,count;
	while(i<1500)
	{
		m=999999999;
		if(m>2*a[j2])m=2*a[j2];
		if(m>3*a[j3])m=3*a[j3];
		if(m>5*a[j5])m=5*a[j5];
		if(m==2*a[j2])j2++;
        if(m==3*a[j3])j3++;
		if(m==5*a[j5])j5++;
		a[i]=m;
		i++;
	}
	while(cin>>count&&count)
	{
		cout<<a[count-1]<<endl;
	}
	return 0;
}

POJ2545:Hamming Problem

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6560   Accepted: 3010

Description

For each three prime numbers p1, p2 and p3, let‘s define Hamming sequence Hi(p1, p2, p3), i=1, ... as containing in increasing order all the natural numbers whose only prime divisors are p1, p2 or p3.

For example, H(2, 3, 5) = 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, ...

So H5(2, 3, 5)=6.

Input

In the single line of input file there are space-separated integers p1 p2 p3 i.

Output

The output file must contain the single integer - Hi(p1, p2, p3). All numbers in input and output are less than 10^18.

Sample Input

7 13 19 100

Sample Output

26590291

和前面的题目一个意思,一开始的问题在于输入不超过10^18,心想这不是搞笑吗,又要TLE了。求出来打表?后来发现输出也要不超过10^18。于是就试试,结果成了。

代码:

#include <iostream>
#pragma warning(disable:4996)
using namespace std;
#define MAXN 10006

long long a[MAXN];

int main()
{
	a[0] = 1;
	int i=1,i2,j1=0,j2=0,j3=0,p1,p2,p3;
	long long m;
	cin>>p1>>p2>>p3>>i2;
	while(i<=10005)
	{
		m=9223372036854775807;
		if(m>p1*a[j1]) m=p1*a[j1];
		if(m>p2*a[j2]) m=p2*a[j2];
		if(m>p3*a[j3]) m=p3*a[j3];

		if(m==p1*a[j1])j1++;
		if(m==p2*a[j2])j2++;
		if(m==p3*a[j3])j3++;
		a[i]=m;
		i++;
	}

	cout<<a[i2]<<endl;
	return 0;
}

POJ2591:Set Definition

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9509   Accepted: 4465

Description

Set S is defined as follows:

(1) 1 is in S;

(2) If x is in S, then 2x + 1 and 3x + 1 are also in S;

(3) No other element belongs to S.

Find the N-th element of set S, if we sort the elements in S by increasing order.

Input

Input will contain several test cases; each contains a single positive integer N (1 <= N <= 10000000), which has been described above.

Output

For each test case, output the corresponding element in S.

Sample Input

100
254

Sample Output

418
1461

代码:

#include <iostream>
#pragma warning(disable:4996)
using namespace std;

int a[10000005];

int main()
{
	a[0] = 1;
	int i=1,j2=0,j3=0;
	long long m;
	while(i<=10000000)
	{
		m=9223372036854775807;
		if(m>2*a[j2])m=2*a[j2]+1;
		if(m>3*a[j3])m=3*a[j3]+1;

		if(m==2*a[j2]+1)j2++;
        if(m==3*a[j3]+1)j3++;
		a[i]=m;
		i++;
	}
	while(scanf("%d",&i)==1)
	{
		cout<<a[--i]<<endl;
	}
	return 0;
}

POJ2247:Humble Numbers

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9951   Accepted: 4651

Description

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.

做到这里就对这种题很烦了。。。

代码:

#include <iostream>
#pragma warning(disable:4996)
using namespace std;
#define MAXN 10006

long long a[MAXN];

int main()
{
	a[1] = 1;
	int i=1,i2,j1=1,j2=1,j3=1,j4=1;
	long long m;

	while(i<=5842)
	{
		m=4000000000;
		if(m>2*a[j1]) m=2*a[j1];
		if(m>3*a[j2]) m=3*a[j2];
		if(m>5*a[j3]) m=5*a[j3];
		if(m>7*a[j4]) m=7*a[j4];

		if(m==2*a[j1])j1++;
		if(m==3*a[j2])j2++;
		if(m==5*a[j3])j3++;
		if(m==7*a[j4])j4++;
		a[++i]=m;
	}
	while(cin>>i2)
	{
		if(!i2)
			break;
		if((i2%100)>=10&&(i2%100)<=20)
		{
			cout<<"The "<<i2<<"th humble number is "<<a[i2]<<"."<<endl;
		}
		else if(i2%10==1)
		{
			cout<<"The "<<i2<<"st humble number is "<<a[i2]<<"."<<endl;
		}
		else if(i2%10==2)
		{
			cout<<"The "<<i2<<"nd humble number is "<<a[i2]<<"."<<endl;
		}
		else if(i2%10==3)
		{
			cout<<"The "<<i2<<"rd humble number is "<<a[i2]<<"."<<endl;
		}
		else
		{
			cout<<"The "<<i2<<"th humble number is "<<a[i2]<<"."<<endl;
		}
	}

	return 0;
}

所以总结一下的话,因为按一条一条的要求逐渐去查找,前一个数又作为查找后一个数的基础,所以有多少条件就搞多少个j1,j2,j3。取最小的那个,之后选择了哪一个条件,就将对应条件的jn+1,让它到队列的下一个,接着判断,逐渐得到一整个数的序列。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-30 06:00:28

POJ1338 & POJ2545 & POJ2591 & POJ2247的相关文章

poj1338 Ugly Numbers(丑数模拟)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1338 Description 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 number

poj1338

Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19952   Accepted: 8856 Description 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 num

POJ1338 Ugly Numbers(解法二)

问题链接:POJ1338 Ugly Numbers.基础级练习题,用C语言编写程序. 题意简述:不能被2.3和5以外的素数整除的数称为丑数,找出第1500个丑数. 问题分析:换句话说,丑数的因子只能是2.3和5.1是丑数,对于x,若x是丑数则2x.3x和5x是丑数.利用已知的丑数,从小到不断生成丑数就可以了. 程序中,结果放在数组ans[]中,也是生产丑数的x:素数放在数组prime[]中,这个问题只有2.3和5:生成的丑数放在数组ugly[]中,然后选出最小的放入结果数组ans[]中,对于被放

POJ1338 Ugly Numbers

问题链接:POJ1338 Ugly Numbers.基础级练习题,用C++语言编写程序. 题意简述:不能被2.3和5以外的素数整除的数称为丑数,找出第1500个丑数. 问题分析:换句话说,丑数的因子只能是2.3和5.1是丑数,对于x,若x是丑数则2x.3x和5x是丑数.利用已知的丑数,从小到不断生成丑数就可以了. 程序中,使用一个STL的容器set来存放丑数.集合具有去重复,自动排序的功能,对于解决本问题是方便的.但是,set对象无法用下标访问,所以倒腾到vector对象中再使用.本问题打表是合

poj2591 Set Definition(递推)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2591 Description Set S is defined as follows: (1) 1 is in S; (2) If x is in S, then 2x + 1 and 3x + 1 are also in S; (3) No other element belongs to S. Find the

4.2 例题: 统计字符数 poj2247

问题描述 判断一个由 a-z 这 26 个字符组成的字符串中哪个字符出现的次数最多 输入:第 1 行是测试数据的组数 n,每组测试数据占 1 行,是一个由 a-z 这 26 个字符组 成的字符串,每组测试数据之间有一个空行,每行数据不超过 1000 个字符且非空 输出:n 行,每行输出对应一个输入.一行输出包括出现次数最多的字符和该字符出现的 次数,中间是一个空格. 如果有多个字符出现的次数相同且最多,那么输出 ascii 码最小的那一个字符. 输入样例 2 abbccc adfadffasdf

UVA 136 &amp; POJ1338 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. 把只含有2.3.5因数的数称为丑数,默认第一个丑数是1. POJ是有多次询问,输出第n个丑数 UVA是询问第1500个丑数是多少. 思路:

Ugly Numbers(POJ1338)(丑数,技巧性强)

Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20851   Accepted: 9248 Description 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 num

【POJ1338】Ugly Numbers(暴力打表)

打表大军是一股邪恶势力→_→ 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <numeric> 8 #include <algorithm> 9 #include <limits.h&g