51nod 1010 只包含因子2 3 5的数 && poj - 1338 Ugly Numbers(打表)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010
http://poj.org/problem?id=1338
首先poj这题要找出第n个丑数,想到要打表,因为每一个丑数都是由前一个丑数*2或者*3或者*5得到,每次取3种结果种较小的那一个放入数组中.
打表的时候要注意保持数组有序.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1010
#define maxv 1010
#define mod 1000000000
using namespace std;
ll p[];
int main()
{
//freopen("a.txt","r",stdin);
int i=,j=,k=;
p[]=;
for(int l=;l<=;l++)
{
p[l]=min(p[i]*,min(p[j]*,p[k]*));
if(p[l]==p[i]*) i++;
if(p[l]==p[j]*) j++;
if(p[l]==p[k]*) k++;
//printf("%lld\n",p[l]);
}
int n;
while(~scanf("%d",&n)&&n)
{
printf("%lld\n",p[n]);
}
return ;
}
51nod这题让你求>=n的丑数,因为n很大,所以打完表后需要二分查找,打表的时候要注意直到大于10^18,上限可以自己试几次.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1010
#define maxv 1010
#define mod 1000000000
using namespace std;
ll p[]; ll erfen(ll n)
{
int l=,r=;
while(l<=r)
{
int mid=(l+r)/;
if(p[mid]==n) return p[mid];
else if(p[mid]>n) r=mid-;
else l=mid+;
}
if(p[l]<n) l++;
return p[l];
}
int main()
{
freopen("a.txt","r",stdin);
int i=,j=,k=;
p[]=;
for(int l=;l<=;l++)
{
p[l]=min(p[i]*,min(p[j]*,p[k]*));
if(p[l]==p[i]*) i++;
if(p[l]==p[j]*) j++;
if(p[l]==p[k]*) k++;
//printf("%lld\n",p[l]);
}
//printf("%lld\n",p[10000]);
int t;
ll n;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
{
if(n==) printf("2\n");
else
printf("%lld\n",erfen(n));
}
}
return ;
}
51nod 1010 只包含因子2 3 5的数 && poj - 1338 Ugly Numbers(打表)的更多相关文章
- 51nod 1010 只包含因子2 3 5的数 二分答案
1010 只包含因子2 3 5的数 K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 > ...
- 51nod 1010 只包含因子2 3 5的数 打表
只包含因子2 3 5的数 题目连接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 Description K的 ...
- 51Nod 1010 只包含因子2 3 5的数 Label:None
K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...
- 51Nod 1010 只包含因子2 3 5的数
K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...
- 51Nod 1010 只包含因子2 3 5的数(打表+二分)
K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...
- 51Nod 1010 只包含因子2 3 5的数 | 预处理+二分
Input示例 5 1 8 13 35 77 Output示例 2 8 15 36 80 分析:将所有的只含有2 3 5因子的数打一个表保存在一个数组里,然后二分查找第一个>=数组里的数,输出 ...
- 1007 正整数分组 1010 只包含因子2 3 5的数 1014 X^2 Mod P 1024 矩阵中不重复的元素 1031 骨牌覆盖
1007 正整数分组 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. Input 第1行:一个 ...
- 只包含因子2 3 5的数(51NOD 1010)
K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...
- 把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> ...
随机推荐
- js学习笔记-事件委托
通过事件委托,你可以把事件处理器绑定到父元素上,避免了把事件处理器添加到多个子级元素上.从而优化性能. 事件代理用到了事件冒泡和目标元素.而任何一个元素的目标元素都是一开始的那个元素. 这里首先要注意 ...
- js插件之Ocupload
使用方法: var myUpload = $(element).upload({ name: 'file', action: '', enctype: 'multipart/form-data', p ...
- JS中的逻辑运算符&&、||,位运算符|,&
1.JS中的||符号: 运算方法: 只要“||”前面为false,不管“||”后面是true还是false,都返回“||”后面的值. 只要“||”前面为true,不管“||”后面是true还是fals ...
- (6)《Head First HTML与CSS》学习笔记---结尾、《HTML5权威指南》读书笔记
1.内联元素的外边距.内边距与块元素稍有不同. 如果一个内联元素四周都增加外边距,只能看到左边和右边会增加空间:你也可以对内联元素的上下增加内边距,不过这个内边距不会影响包围它的其他内联元素的间距—— ...
- windows上把git生成的ssh key
右键鼠标,选中 “Git Bash here”: 输入指令,创建ssh key: cd ~/.ssh/ #bash: cd: /c/Users/Administrator/.ssh/: No such ...
- iOS:swift :可选类型
import UIKit /*: 可选类型 * 可选类型表示变量可以有值, 也可以没有值 * C 和 Objective-C 中并没有可选类型这个概念 * Swift中只有可选类型才可以赋值为nil ...
- vue热重载
依据官网使用 webpack 的 Hot Module Replacement API,Vuex 支持在开发过程中热重载 mutation.module.action 和 getter.你也可以在 B ...
- leetcode_486. Predict the Winner
https://leetcode.com/problems/predict-the-winner/ 题目描述:给定一个非负的积分数组,玩家1可以从数组两端任取一个积分,接着玩家2执行同样的操作,直至积 ...
- discuz 插件核心函数hookscript分析.
function hookscript($script, $hscript, $type = 'funcs', $param = array(), $func = '', $scriptextra = ...
- Jmeter之https请求
Jmeter之录制https脚本,网上介绍了好多种方法,大家自行百度. 如果手写https脚本,该如何做呢? 方法:http信息头管理器,加入User-Agent参数 案例:手写百度的搜索:哈哈 请 ...