Diverse Permutation

Codeforces Round #275 (Div. 1)   A. Diverse Permutation

链接:http://codeforces.com/contest/482/problem/A

解题思路:构造。构造1,n,2,n-1,3,。。。   这俩俩之间的绝对值差为n-1,n-2,n-3,。。。  

则所有绝对值差都不同,当有k-1个不同的绝对值差时,就停止,将剩下的中间数顺序输出,最后俩俩之间的绝对值差都为1,就有k个不同绝对值差。

c++ 代码如下:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int n,k;
 9     while(scanf("%d%d",&n,&k)!=EOF)
10     {
11         int i;
12         int s=1;
13         int f=0;
14         while(1)
15         {
16             printf("%d ",s);
17             k--;
18             if(k==0)
19             {
20                 f=1;
21                 break;
22             }
23             printf("%d ",n+1-s);
24             k--;
25             if(k==0)
26             {
27                 f=2;
28                 break;
29             }
30             s++;
31         }
32         if(f==1)
33         {
34             for(i=s+1;i<=n+1-s;i++)
35             {
36                 printf("%d ",i);
37             }
38         }
39         else
40         {
41             for(i=n-s;i>s;i--)
42             {
43                 printf("%d ",i);
44             }
45         }
46         printf("
");
47     }
48 
49 
50 
51     return 0;
52 }

AC如下:

原文地址:https://www.cnblogs.com/sunjieee/p/4055494.html