问题 K: WaWa的难题

问题 K: WaWa的难题

时间限制: 1 Sec  内存限制: 128 MB
提交: 570  解决: 125
[提交] [状态] [命题人:jsu_admin]

题目描述

HaHa和WaWa是好朋友,他们在临近期末的这段时间一起宅在图书馆学习。
今天HaHa在书上看到一个排列组合题目,思考很久后,仍然找不出其中的规律。
于是他把题目叙述给了WaWa。
题目:
————————————————————————
一个长度为N的排列,由数字1~N组成,它满足两个条件。
1、数字1永远在第一位。
2、任意两个相邻数字之差小于等于2。
现在给出一个N,
你能知道能组成多少个符合条件的排列吗?。
例如:
N=4
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
所以答案为4
————————————————————————
WaWa听后也是一脸懵逼。
现在WaWa想求助于你们,WaWa给出一个正整数N,问你用1~N能组成多少个符合题意的排列。
 

输入

多组数据。
每组数据输入一个正整数N(1<=N<=100)。

输出

输出符合题意的排列个数

样例输入 Copy

2
4

样例输出 Copy

1
4

 

dfs 打表前 10 项即可发现规律

a[i]=a[i-1]+a[i-3]+1

 1 #include<stdio.h>
 2 int num = 0;
 3 int sum = 0;
 4 void dfs(int n,int a){
 5     if(sum == n)
 6     {
 7         num ++;
 8         return ;    
 9     }
10     dfs(n,a++);
11 
12 }
13 int main()
14 {
15     int n;
16     long long a[1000];
17     a[1]=1;
18     a[2]=1;
19     a[3]=2;
20     a[4]=4;
21     for(int i=5;i<=100;i++)
22         a[i]=a[i-1]+a[i-3]+1;
23     while(scanf("%d",&n)!=EOF)    
24     {
25     //    num = 0;
26     //    dfs(n,1);
27         printf("%lld\n",a[n]);
28     }
29 }
View Code

打表

 1 #include <bits/stdc++.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<cstdio>
 7 #include<string>
 8 #include<vector>
 9 #include<bitset>
10 #include<queue>
11 #include<deque>
12 #include<stack>
13 #include<cmath>
14 #include<list>
15 #include<map>
16 #include<set>
17 //#define DEBUG
18 #define RI register int
19 using namespace std;
20 typedef long long ll;
21 //typedef __int128 lll;
22 const int N=1000;
23 const int MOD=1e9+7;
24 const double PI = acos(-1.0);
25 const double EXP = 1E-8;
26 const int INF = 0x3f3f3f3f;
27 int t,n,m,k,q;
28 int ans=0;
29 int vis[110];
30 void dfs(int x,int c){
31     if(c>=n){
32         ans++;
33         return;
34     }
35     int l=max(1,x-2);
36     int r=min(n,x+2);
37     for(int i=l;i<=r;i++){
38         if(!vis[i]){
39             vis[i]=1;
40             dfs(i,c+1);
41             vis[i]=0;
42         }
43     }
44 }
45 int main()
46 {
47 #ifdef DEBUG
48     freopen("input.in", "r", stdin);
49     //freopen("output.out", "w", stdout);
50 #endif
51     ll a[N];
52     a[1]=1;
53     a[2]=1;
54     a[3]=2;
55     a[4]=4;
56     for(int i=5;i<=100;i++)
57         a[i]=a[i-1]+a[i-3]+1;
58     while(~scanf("%d",&n)){
59         //ans=0;
60         //memset(vis,0,sizeof(vis));
61         //vis[1]=1;
62         //dfs(1,1);
63         cout << a[n] << endl;
64     }
65     //cout << "Hello world!" << endl;
66     return 0;
67 }
View Code

猜你喜欢

转载自www.cnblogs.com/DWVictor/p/10202517.html