擅长排列的小明 II

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述

小明十分聪明,而且十分擅长排列计算。

有一天小明心血来潮想考考你,他给了你一个正整数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的更多相关文章

  1. 擅长排列的小明 II(找规律)

    擅长排列的小明 II 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 小明十分聪明,而且十分擅长排列计算. 有一天小明心血来潮想考考你,他给了你一个正整数n,序列1,2, ...

  2. nyoj 19擅长排列的小明 (DFS)

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...

  3. nyoj 19 擅长排列的小明(深搜,next_permutation)

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...

  4. NYOJ469 - 擅长排列的小明 II - (dp)

    题目描述: 小明十分聪明,而且十分擅长排列计算. 有一天小明心血来潮想考考你,他给了你一个正整数n,序列1,2,3,4,5......n满足以下情况的排列: 1.第一个数必须是1 2.相邻两个数之差不 ...

  5. NYOJ-469 擅长排列的小明 II AC 分类: NYOJ 2014-01-02 22:19 159人阅读 评论(0) 收藏

    最初的第一印象是和组合数一个性质的题目.所以用了回溯法,结果,你懂的... #include<stdio.h> #include<math.h> void dfs(int n, ...

  6. 擅长排列的小明II

    先搜索 出来一点结果之后  看结果之间的 联系 得出  递推公式  . #include<stdio.h> #include<string.h> #include<mat ...

  7. nyoj 题目19 擅长排列的小明

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...

  8. ACM 擅长排列的小明

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...

  9. nyoj-0469-擅长排列的小明 II(找规律)

    nyoj-0469-擅长排列的小明 II 思路:递推分析:为了简便起见,我们用Ai代表第i个数字 , 由于A1一直是1,所以A2只能是2或3.假设dp[n]表示1->n这个序列的方案数      ...

随机推荐

  1. h5画圆

    下面一段代码是,h5的画圆,半圆,四分之一圆等效果 <!DOCTYPE html> <html lang="en"> <head> <me ...

  2. thinkphp分页集成

    控制器: $User = M('webcase'); //  实例化 User 对象 $list = $User->order('id desc')->page($_GET['p'].', ...

  3. Jenkins环境搭建(6)-修改自动化测试报告的样式

    写在最前: 我遇到一个问题,就是导出数据时,接口返回的数据是乱码,乱码如下图所示.问了开发,说是byte数据.这种情况,将response Data数据写入到报告中的话,在jenkins上运行时,提示 ...

  4. Android 如何利用Activity的Dialog风格完成弹出框设计

    在我们使用Dialog时,如果需要用到很多自己设计的控件,虽然可以让弹出框显示出我们需要的界面,但却无法找到地方完成控制代码的编写,如何解决这个问题呢,我们可以将Activity伪装成Dialog弹出 ...

  5. 使用POI创建word表格合并单元格兼容wps

    poi创建word表格合并单元格代码如下: /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, ...

  6. IOS生成UUID

    /** * 生成GUID */ + (NSString *)generateUuidString{ // create a new UUID which you own CFUUIDRef uuid ...

  7. strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用

    strong关键字与retain关似,用了它,引用计数自动+1,用实例更能说明一切 @property (nonatomic, strong) NSString *stringA; @property ...

  8. Asp.Net Core 入门(五)—— 布局视图_Layout.cshtml

    布局视图和我们在Asp.Net MVC一样,布局视图_Layout.cshtml使得所有视图保持一致的外观变得更加容易,因为我们只有一个要修改的布局视图文件,更改后将立即反映在整个应用程序的所有视图中 ...

  9. java 正则表达式如何提取中文的问题

    String regex="([\u4e00-\u9fa5]+)"; String str="132更新至456"; Matcher matcher = Pat ...

  10. Codeforces Round #273 (Div. 2)-C. Table Decorations

    http://codeforces.com/contest/478/problem/C C. Table Decorations time limit per test 1 second memory ...