四道题的难度: 2591<1338<2545<2247

POJ 2591 Set Definition:

这是从discuss里看来的,写的挺好,直接copy,根据我的代码稍有改动(其实也就只改动了一个数字):

题目所给的是个简单的双递归。

我们注意到任何新的元素必然是集合中较小的元素经过两种之一的递推得到的。
这启发我们两个设置标记(比如two,three两个变量):第一个是以方式乘2加1递推,two是没有用过的最小项下标,
第二则表示以乘3加1的方式递推,three是没有用过的最小项下标。开始two =0,three=0,都指向最小的元素0。
然后比较两种递推得到的结果,取较小的放到数组中(这样就得到了一个新的集合元素),并更新相应的下标。
如果相同,只放进去一个,两个下标同时更新。这样可以线性的求出前N个元素。

#include <iostream>
#include <stdio.h>
#include <map>
#include <string.h>
#include <algorithm> using namespace std;
const long maxn=;
long num[maxn];
int two,three;
int idx;
void init(){
memset(num,,sizeof(num));
idx=;
num[]=;
two=three=;
for(int i=;i<maxn;i++){
if(*num[two]==*num[three]){
num[++idx]=*num[two]+;
two++;
three++;
}
else if(*num[two]<*num[three]){
num[++idx]=*num[two]+;
two++;
}
else{
num[++idx]=*num[three]+;
three++;
}
}
}
int main()
{
init();
int n;
while(scanf("%d",&n)!=EOF){
printf("%d\n",num[n]);
}
return ;
}

附上discuss里大牛的写法:

#include<stdio.h>
#define Min(a, b) ((a)<(b)?(a):(b))
#define N 10000001
long f[N];
int main()
{
int n,i;
int x_2 = ,x_3 = ;
f[x_2] = ;
f[x_3] = ;
for(i=; i<=; i++)
{
f[i] = Min(f[x_2]*+, f[x_3]*+); if(f[i] == f[x_2]*+)
x_2++;
if(f[i] == f[x_3]*+)
x_3++;
}
while(scanf("%d",&n) != EOF)
{
printf("%ld\n",f[n]);
}
return ;
}

接下来三道题,和上面类似,就不说了,直接附上代码:

POJ 1338 Ugly Numbers

#include <iostream>
#include <stdio.h>
#include <map>
#include <string.h>
#include <algorithm> using namespace std;
const long maxn=;
long num[maxn];
int two,three,five; void init(){
memset(num,,sizeof(num));
num[]=;
two=three=five=;
for(int i=;i<maxn;i++){
num[i]=min(*num[two],min(*num[three],*num[five]));
if(num[i]==*num[two])
two++;
if(num[i]==*num[three])
three++;
if(num[i]==*num[five])
five++;
}
}
int main()
{
int n;
init();
while(scanf("%d",&n),n){
printf("%d\n",num[n]);
}
return ;
}

POJ 2545 Hamming Problem

#include <iostream>
#include <stdio.h>
#include <map>
#include <string.h>
#include <algorithm>
#include <string>
/*
本来抱着试一试的态度,再获取n的大小(也就是题目中的i)之后,再建立数组。
本来以为会RE,没想到AC。。。
后来在discuss中看到有人说,在满足output<10^18的情况下,用 2,3,5算出来的最大i大概在10000左右
晕。。。 */
using namespace std;
long long p1,p2,p3;
long long pp1,pp2,pp3;
long long n; int main()
{
scanf("%I64d%I64d%I64d%I64d",&p1,&p2,&p3,&n);
long long *num;
num=new long long[n+];
num[]=;
pp1=pp2=pp3=;
for(int i=;i<=n;i++){
num[i]=min(p1*num[pp1],min(p2*num[pp2],p3*num[pp3]));
if(num[i]==p1*num[pp1])
pp1++;
if(num[i]==p2*num[pp2])
pp2++;
if(num[i]==p3*num[pp3])
pp3++;
}
cout<<num[n]<<endl;
return ;
}

POJ 2247 Humble Numbers

#include <iostream>
#include <stdio.h>
#include <map>
#include <string.h>
#include <algorithm>
#include <string> //刚开始忘记导入了。。。 using namespace std;
const long maxn=;
long long num[maxn];
int two,three,five,seven;
map<int,string> form;
void init(){
memset(num,,sizeof(num));
num[]=;
two=three=five=seven=;
for(int i=;i<maxn;i++){
num[i]=min(*num[two],min(*num[three],min(*num[five],*num[seven])));
if(num[i]==*num[two])
two++;
if(num[i]==*num[three])
three++;
if(num[i]==*num[five])
five++;
if(num[i]==*num[seven])
seven++;
}
form[]="th";
form[]="st";
form[]="nd";
form[]="rd";
form[]="th";
form[]="th";
form[]="th";
form[]="th";
form[]="th";
form[]="th";
//注意下面三个。。。
form[]="th";
form[]="th";
form[]="th"; }
int main()
{
int n;
init();
while(scanf("%d",&n),n){
int tmp1=n%,tmp2=n%; //额额,一开始就只考虑了两位数的情况。。。
if(tmp2==||tmp2==||tmp2==)
cout<<"The "<<n<<form[tmp2]<<" humble number is "<<num[n]<<"."<<endl;
else if(tmp1==||tmp1==||tmp1==)
cout<<"The "<<n<<form[tmp1]<<" humble number is "<<num[n]<<"."<<endl;
else{
cout<<"The "<<n<<form[tmp1]<<" humble number is "<<num[n]<<"."<<endl;
}
}
return ;
}

POJ 2591 1338 2545 2247(数列递归衍生问题,思路挺妙)的更多相关文章

  1. day18 时间:time:,日历:calendar,可以运算的时间:datatime,系统:sys, 操作系统:os,系统路径操作:os.path,跨文件夹移动文件,递归删除的思路,递归遍历打印目标路径中所有的txt文件,项目开发周期

    复习 ''' 1.跨文件夹导包 - 不用考虑包的情况下直接导入文件夹(包)下的具体模块 2.__name__: py自执行 '__main__' | py被导入执行 '模块名' 3.包:一系列模块的集 ...

  2. Reverse反转算法+斐波那契数列递归+Reverse反转单链表算法--C++实现

    Reverse反转算法 #include <iostream> using namespace std; //交换的函数 void replaced(int &a,int & ...

  3. POJ 1321-棋盘问题(DFS 递归)

    POJ 1321-棋盘问题 K - DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  4. PHP算法之斐波那契数列(递归)

    /*斐波那契数列 源代码分析 f(x) = 1 ; 当 x < 2 ; f(x) = f(x-1)+f(x-2); 当 x >= 2 ; 通项式为:fn ={((1+根号5)/2)^n-( ...

  5. python之斐波那契数列递归推导在性能方面的反思

    在各种语言中,谈到递归首当其冲的是斐波那契数列,太典型了,简直就是标杆 一开始本人在学习递归也是如此,因为太符合逻辑了 后台在工作和学习中,不断反思递归真的就好嘛? 首先递归需要从后往前推导,所有数据 ...

  6. UVaLive 3357 Pinary (Fib数列+递归)

    题意:求第 k 个不含前导 0 和连续 1 的二进制串. 析:1,10,100,101,1000,...很容易发现长度为 i 的二进制串的个数正好就是Fib数列的第 i 个数,因为第 i 个也有子问题 ...

  7. POJ 1780 Code(欧拉回路+非递归dfs)

    http://poj.org/problem?id=1780 题意:有个保险箱子是n位数字编码,当正确输入最后一位编码后就会打开(即输入任意多的数字只有最后n位数字有效)……要选择一个好的数字序列,最 ...

  8. java递归 斐波那契数列递归与非递归实现

    递归简单来说就是自己调用自己, 递归构造包括两个部分: 1.定义递归头:什么时候需要调用自身方法,如果没有头,将陷入死循环 2.递归体:调用自身方法干什么 递归是自己调用自己的方法,用条件来判断调用什 ...

  9. [剑指offer] 7. 斐波那契数列 (递归 时间复杂度)

    简介: 杨辉三角每条斜线上的数之和就构成斐波那契数列. 思路: 参考文章:https://mp.weixin.qq.com/s?src=11&timestamp=1551321876& ...

随机推荐

  1. NOJ1008-第几天

    第几天 时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte总提交 : 2701            测试通过 : 800  ...

  2. 6.24 AppCan移动开发者大会,我爱我家即将闪亮登场!

    6.24 AppCan移动开发者大会进入倒计时,报名通道即将关闭! “6月24日, 2016AppCan移动开发者大会即将召开,以“平台之上,应用无限”为主题,1500位行业精英汇聚在此,重磅新品发布 ...

  3. hdu 1496 Equations

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 Equations Description Consider equations having ...

  4. net-snmp的安装

    安装环境是ubuntu 14. 方法1:apt-get install  net-snmp (非root用户需要sudo 提升权限) 方法2:自定义安装选择不同的版本去编译. 1:先去下载所需要的ta ...

  5. Go字符串函数

    下面的代码中,列出了Go官方包中常见的字符串函数. package main import s "strings" import "fmt" //为打印函数起个 ...

  6. DSP28335的SPI发送

    #include "DSP2833x_Device.h"#include "DSP2833x_Examples.h"unsigned char table[]= ...

  7. 微软职位内部推荐-SDEII

    微软近期Open的职位: Software Engineer II for Customer Experience (Level 62+) Location: Suzhou Contact Perso ...

  8. Ionic入门一:Hello Ionic

    1.在终端里面进入准备存放App的目录:  2.Ionic官网提供了三个项目模板blank.tabs和sideMenu ,用“ionic start myApp tabs”创建ionic项目:  ...

  9. 如何在Android模拟器上安装apk文件

    1.运行SDK Manager,选择模拟器,并运行模拟器 SDK Manager应用 2.将需要安装的apk文件复制到platform-tools目录下(默认在:D:\tools\android\ad ...

  10. C/C++求职宝典21个重点笔记

    转:http://blog.csdn.net/lanxuezaipiao/article/details/41557307 char c = '\72'; 中的\72代表一个字符,72是八进制数,代表 ...