2019 年百度之星·程序设计大赛 - 初赛一 1005 Seq(数学规律)

 http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=861&pid=1005

Sample Input

5
1
2
3
4
5

Sample Output

1
1
0
3
0

这题第一反应打表,先计算,后查询输出,后来注意到数据太大,行不通

索性把打好的表输出来找规律,果然有规律可循

奇数和偶数有不同的规律,但周期都是3

大家可以把下面的代码跑一下,分别输出1到30之间的奇数和偶数项的结果,试着自己找一找规律

扫描二维码关注公众号,回复: 7051679 查看本文章

代码如下:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <set>
 9 #include <math.h>
10 const int INF=0x3f3f3f3f;
11 typedef long long LL;
12 const int mod=1e9+7;
13 const double PI=acos(-1);
14 const int maxn=1e5+10;
15 using namespace std;
16 
17 
18 int main()
19 {
20     int T;
21     scanf("%d",&T);
22     while(T--)
23     {
24         LL n;
25         scanf("%lld",&n);
26         if(n&1==1)//奇数项 
27         {
28             LL num,a,b;
29             int p,q;
30             num=(n+1)/2;//算一下是第几个奇数 
31             p=1;
32             q=0;
33             a=num/3;
34             b=num%3;
35             if(b==1)
36                 printf("%lld\n",p+4*a);
37             else if(b==2)
38                 printf("%lld\n",q+a);
39             else if(b==0)
40                 printf("%lld\n",q+a-1);
41         }
42         else//偶数项 
43         {
44             LL num,a,b;
45             num=n/2;//算一下是第几个偶数 
46             if(n==2) //把2当做了特殊项,便于以后计算 
47                 printf("1\n");
48             else
49             {
50                 a=(num-1)/3;
51                 b=(num-1)%3;
52                 if(b==1)
53                     printf("%lld\n",n-1);
54                 else
55                     printf("%lld\n",n/2);
56             }
57         }
58     }
59     return 0;
60 }

猜你喜欢

转载自www.cnblogs.com/jiamian/p/11371016.html