NYOJ 469 擅长排列的小明 II
擅长排列的小明 II
- 描述
-
小明十分聪明,而且十分擅长排列计算。
有一天小明心血来潮想考考你,他给了你一个正整数n,序列1,2,3,4,5......n满足以下情况的排列:
1、第一个数必须是1
2、相邻两个数之差不大于2
你的任务是给出排列的种数。
- 输入
- 多组数据。每组数据中输入一个正整数n(n<=55).
- 输出
- 输出种数。
- 样例输入
-
4
- 样例输出
-
4
- 来源
- Ural
- 上传者
- 李文鑫
-
解题:俺找规律才找出来的,开始写了个暴力搜索,输出前10项结果
1 1
2 1
3 2
4 4
5 6
6 9
7 14
8 21
9 31
10 46
找到规律了吧。哈哈 先上超时代码:#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
int d[],n,cnt;
bool used[];
void dfs(int cur,int pre){
if(cur > n) {
cnt++;
return;
}
int x = pre-, y = pre+;
for(x; x <= y && x <= n; x++){
if(x >= && !used[x]){
used[x] = true;
dfs(cur+,x);
used[x] = false;
}
}
}
int main(){
while(~scanf("%d",&n)){
cnt = ;
memset(used,false,sizeof(used));
used[] = true;
for(int i = ; i <= n; i++) d[i] = i;
dfs(,);
printf("%d\n",cnt);
}
return ;
}AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
int main(){
int ans[] = {,,,,,},i;
for(i = ; i <= ; i++)
ans[i] = ans[i-]+ans[i-]+;
while(~scanf("%d",&i)) printf("%d\n",ans[i]);
return ;
}
NYOJ 469 擅长排列的小明 II的更多相关文章
- 擅长排列的小明 II(找规律)
擅长排列的小明 II 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 小明十分聪明,而且十分擅长排列计算. 有一天小明心血来潮想考考你,他给了你一个正整数n,序列1,2, ...
- nyoj 19擅长排列的小明 (DFS)
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...
- nyoj 19 擅长排列的小明(深搜,next_permutation)
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...
- NYOJ469 - 擅长排列的小明 II - (dp)
题目描述: 小明十分聪明,而且十分擅长排列计算. 有一天小明心血来潮想考考你,他给了你一个正整数n,序列1,2,3,4,5......n满足以下情况的排列: 1.第一个数必须是1 2.相邻两个数之差不 ...
- NYOJ-469 擅长排列的小明 II AC 分类: NYOJ 2014-01-02 22:19 159人阅读 评论(0) 收藏
最初的第一印象是和组合数一个性质的题目.所以用了回溯法,结果,你懂的... #include<stdio.h> #include<math.h> void dfs(int n, ...
- 擅长排列的小明II
先搜索 出来一点结果之后 看结果之间的 联系 得出 递推公式 . #include<stdio.h> #include<string.h> #include<mat ...
- nyoj 题目19 擅长排列的小明
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...
- ACM 擅长排列的小明
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...
- nyoj-0469-擅长排列的小明 II(找规律)
nyoj-0469-擅长排列的小明 II 思路:递推分析:为了简便起见,我们用Ai代表第i个数字 , 由于A1一直是1,所以A2只能是2或3.假设dp[n]表示1->n这个序列的方案数 ...
随机推荐
- 剖析 Rails 3 MVC 中的数据传递
引用链接:https://www.ibm.com/developerworks/cn/web/1108_linhx_rails3mvc/ 如果读者已经开发过基于 Rails 的应用,但对其 MVC 间 ...
- Java之final、static关键字及匿名对象
个人通俗理解: 1.final:首先被final修饰的变量就自动变成的不能被修改的常量了.被修饰的类会自动变成太监类,只能有父类,不能有子类:被修饰的方法也不能被子类重写了:被修饰的引用变量值也不能更 ...
- 一个普通Java程序包含哪些线程??
package com.java.threads; import java.lang.management.ManagementFactory; import java.lang.management ...
- vue从入门到开发--3-基础语法
一:v-text指令 指令v-text:可通过该指令绑定动态数据(动态数据我觉得可以是从服务器请求下来的数据,保存在data里边,然后动态显示在文档中,因为是一个一个的组件,数据应该不会很多吧,就只有 ...
- be seen doing和be seen to do的区别
1. be seen doing和be seen to do的区别 be seen doing表被看到正在做某事:be seen to do 表被看到做某事(不表进行) He was seen to ...
- sysdig安装和使用介绍
安装步骤1)安装资源库rpm --import https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.publiccurl -s -o ...
- [文章泛读] The varying faces of a program transformation systems (ACM Inroads, 2012)
Beevi S. Nadera, D. Chitraprasad, and Vinod S. S. Chandra. 2012. The varying faces of a program tran ...
- vba 时间
Sub tt1() Dim d1, d2 As Date d1 = #//# d2 = #//# Debug.Print "相隔" & (d2 - d1) & &q ...
- jni log 使用
1. 在源文件中添加头文件 #include <android/log.h> #define LOG_TAG "System.out.c" #define LOGD(. ...
- poj2312Battle City BFS
题意: M行N列矩阵, 'Y'表示开始位置, 'T'表示目标位置, 从开始位置到目标位置至少需要走多少步,其中, 'S', 'R'表示不能走, 'B' 花费为2, 'E'花费为1. 思路:纯 BFS. ...