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

Sample Input


Sample Output


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

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

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

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

代码如下:

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const double PI=acos(-);
const int maxn=1e5+;
using namespace std; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
LL n;
scanf("%lld",&n);
if(n&==)//奇数项
{
LL num,a,b;
int p,q;
num=(n+)/;//算一下是第几个奇数
p=;
q=;
a=num/;
b=num%;
if(b==)
printf("%lld\n",p+*a);
else if(b==)
printf("%lld\n",q+a);
else if(b==)
printf("%lld\n",q+a-);
}
else//偶数项
{
LL num,a,b;
num=n/;//算一下是第几个偶数
if(n==) //把2当做了特殊项,便于以后计算
printf("1\n");
else
{
a=(num-)/;
b=(num-)%;
if(b==)
printf("%lld\n",n-);
else
printf("%lld\n",n/);
}
}
}
return ;
}

最新文章

  1. 转载文档:Storm实战常见问题及解决方案
  2. URLDecoder解析url编码
  3. OpenCV2马拉松第22圈——Hough变换直线检測原理与实现
  4. C读写配置文件
  5. C语言基础学习学习前的准备-2
  6. grok 官方文档
  7. C#设计模式之二十二访问者模式(Visitor Pattern)【行为型】
  8. Spring 的IOC和AOP总结
  9. &lt;算法图解&gt;读书笔记:第2章 选择排序
  10. 边缘检测之Canny
  11. Vultr新用户充值优惠 – 最多充值100美元送100美元
  12. 第86节:Java中的JQuery基础
  13. [Linux 性能调优] 网卡中断与CPU的绑定问题
  14. DL_1_week1_概论
  15. Easyui和IE浏览器的兼容问题
  16. 十三、MVC的WEB框架(Structs2)
  17. wampserver的配置教程
  18. runOnUiThread更新主线程
  19. Codeforces 862C - Mahmoud and Ehab and the xor
  20. eclipse从svn导入maven项目变成普通项目解决办法

热门文章

  1. 建立更可靠的OOP程序-类和成员的访问控制
  2. POJ 1160:Post Office 邮局经典DP
  3. find_element_by_xpath()的6种方法
  4. jquery 第二节 Dom和jQuery的互相转换
  5. 时间复杂度T(n)
  6. 201509-2 日期计算 Java
  7. java的io字符流关闭和刷新.flush();
  8. Linux学习(二) --- 常用命令
  9. Tomcat8 启动报错
  10. 关于Ueditor富文本编辑器的配置和使用心得