加油 ( •̀ ω •́ )y!

HDU 5916 Harmonic Value Description

Problem Description
The harmonic value of the permutation p1,p2,pn is

 


Mr. Frog is wondering about the permutation whose harmonic value is the strictly k-th smallest among all the permutations of [n].
 

 

Input
The first line contains only one integer T (1T100), which indicates the number of test cases.

For each test case, there is only one line describing the given integers n and k (12kn10000).
 

 

Output
For each test case, output one line “Case #x: p1 p2  pn”, where x is the case number (starting from 1) and p1 p2  pn is the answer.
 

 

Sample Input
2
4 1
4 2
 

 

Sample Output
Case #1: 4 1 3 2
Case #2: 2 4 1 3
 
 
 题目大意:给定一个数n,k,求出一个序列,使得他的和是第k小的(序列为1-n)

思路: 找出规律即可,当k=1时,直接按顺序输出1-n;k>1时 讲前2*k个数中的偶数和奇数分别放在一起,最后再将2*k+1 --- n 顺序输出即可

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 
 5 using namespace std;
 6 int T,n,k;
 7 int main()
 8 {
 9     scanf("%d",&T);
10     for(int t=1;t<=T;t++){
11         scanf("%d%d",&n,&k);
12         printf("Case #%d:",t);
13         if(k>1){
14             for(int i=2;i<=2*k;i+=2)
15                 printf(" %d",i);
16             for(int i=1;i<2*k;i+=2)
17                 printf(" %d",i);
18             for(int i=2*k+1;i<=n;i++)
19                 printf(" %d",i);
20         }else{
21             for(int i=1;i<=n;i++)
22                 printf(" %d",i);
23         }
24         putchar('\n');
25     }
26     return 0;
27 }

 

 

posted @ 2018-09-09 20:23  皮皮虎  阅读(171)  评论(0编辑  收藏  举报