Project Euler 45 Triangular, pentagonal, and hexagonal( 二分 + 函数指针 )
题意:
三角形数、五边形数和六角形数分别由以下公式给出:
三角形数 | Tn=n(n+1)/2 | 1, 3, 6, 10, 15, … |
五边形数 | Pn=n(3n−1)/2 | 1, 5, 12, 22, 35, … |
六边形数 | Hn=n(2n−1) | 1, 6, 15, 28, 45, … |
可以验证,T285 = P165 = H143 = 40755。
找出下一个同时是三角形数、五边形数和六角形数的数。
思路:因为六角形数增长速度最快,所以去枚举六角形数,然后二分一下这个数是否在三角形数和五角形数之中出现过。利用函数指针可以降低代码长度,免去了在二分中增加判断是从三角形数中查找还是五角形数中查找这一操作。
函数指针链接:****Here!
/*************************************************************************
> File Name: euler045.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月27日 星期二 14时09分07秒
************************************************************************/
#include <stdio.h>
#include <inttypes.h>
typedef int64_t (*calcFunc)(int64_t); // 声明函数指针降低代码长度
// 声明了一个名为calcFunc,返回值为int64_t,函数参数列表为一个int64_t形参的指针变量
int64_t Hexagonal (int64_t n) {
return n * (2 * n - 1) ;
}
int64_t Triangle (int64_t n) {
return (n * (n + 1)) / 2;
}
int64_t Pentagonal (int64_t n) {
return (n *(3 * n - 1)) / 2;
}
bool Binary_Serch (int64_t n , calcFunc f) { // 二分查找一下n
int64_t l = 1 , r = n , mid;
while (l < r) {
mid = (l + r) >> 1;
if (f(mid) < n) l = mid + 1;
else r = mid;
}
return f(r) == n;
}
int32_t main() {
int64_t i = 143 , t;
while (cnt) {
t = Hexagonal((++i));
if (!Binary_Serch(t , Triangle)) continue;
if (!Binary_Serch(t , Pentagonal)) continue;
printf("%"PRId64"\n",t);
break;
}
return 0;
}
Project Euler 45 Triangular, pentagonal, and hexagonal( 二分 + 函数指针 )的更多相关文章
- Project Euler 363 Bézier Curves(几何+二分)
题目链接: https://projecteuler.net/problem=363 题目: A cubic Bézier curve is defined by four points: \(P_0 ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
- Python练习题 040:Project Euler 012:有超过500个因子的三角形数
本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divi ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
- project euler 169
project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...
- 【Project Euler 8】Largest product in a series
题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...
随机推荐
- UVa - 11283 - PLAYING BOGGLE
先上题目 Problem F PLAYING BOGGLE Boggle® is a classic word game played on a 4 by 4 grid of letters. The ...
- 1009在WINDOWS上面如何备份数据库
第一步 解决windows上面使用GZIP命令 参考http://www.xuebuyuan.com/1676976.html 后经研究,发现解决方法也很简单,只需下载gzip的windows版本,解 ...
- 洛谷 P1560 [USACO5.2]蜗牛的旅行Snail Trails(不明原因的scanf错误)
P1560 [USACO5.2]蜗牛的旅行Snail Trails 题目描述 萨丽·斯内尔(Sally Snail,蜗牛)喜欢在N x N 的棋盘上闲逛(1 < n <= 120). 她总 ...
- POJ2391 Ombrophobic Bovines 网络流拆点+二分+floyed
题目链接: id=2391">poj2391 题意: 有n块草地,每块草地上有一定数量的奶牛和一个雨棚,并给出了每一个雨棚的容(牛)量. 有m条路径连接这些草地 ,这些路径是双向的, ...
- T2: 一种能累积计算积分的EC2实例类型
假设您打算在AWS云端执行一个小型的 Web Server,或是一个小型的数据库,平时并没有大量的工作负载.在绝大多数时间里,您的实例并不须要消耗大量的CPU资源.可是,再不怎么受欢迎的博客也可能会有 ...
- 黑马程序猿——JAVA基础——集合
----------android培训.java培训.java学习型技术博客.期待与您交流.------------ 一.关于java中的集合类 首先看一下,大致的框架流程图 ...
- MySQL出现Ignoring query to other database的问题
今天使用mysql的时候,输入随意一条命令都会出: Ignoring query to other database 这条错误信息,非常是奇怪. 后来才发现是登录数据库时.少了个-u的參数.. 正确的 ...
- hdoj--3072--Intelligence System(scc+缩点+数据去重)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- golang 初体验
1.下载golang https://code.google.com/p/go/downloads/list 在windows下安装,下载windows32版本 2.安装 安装完毕,默认在C:\Go ...
- ubantu下NiFi单节点安装部署
第一步,首先下载安装包:http://nifi.apache.org/download.html,博主下载的是1.4.0版本,直接下载的是编译后的文件. 第二步:将压缩包上传到服务器相应目录下,并且解 ...