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 × ...
随机推荐
- [bzoj1218][HNOI2003]激光炸弹_暴力
激光炸弹 bzoj-1218 HNOI-2003 题目大意:在笛卡尔坐标系上有n个点,问一个平行于坐标轴的r*r的正方形可以最多覆盖多少个目标. 注释:$1\le n \le 10000$,$1\le ...
- Maven错误:[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?的解决方法
错误: [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather tha ...
- 【日常学习】【搜索/递归】codevs2802 二的幂次方题解
转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看 题目描写叙述 Description 不论什么一个正整数都能够用2的幂次方表示. 比如:13 ...
- uva 10276 / pc 110908
黑书上说用二分图的知识来解,但我想不出来,只好找规律 发现,一条柱时为1,两条柱时为4.三条柱时为8.. 这些1,3,7,11的数字加1后,都是下一条柱的最底部的数字,而且一条柱的数字之和总是按照这样 ...
- hdu 1011 树型dp
#include <cstdio> #include <iostream> #include <cstring> #include <vector> u ...
- D 分组背包
<span style="color:#3333ff;">/* ---------------------------------------------------- ...
- sql处理高并发
sql处理高并发,防止库存超卖 2014-08-14 23:44 13560人阅读 评论(2) 收藏 举报 分类: 数据库(43) 今天王总又给我们上了一课,其实mysql处理高并发,防止库存超卖 ...
- ACM-并查集之小希的迷宫——hdu1272
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- <% %> in html
$(document).on('click', '.invoiceNumber', function () { var string = <%= StaticHelper.GetCurrentC ...
- [BZOJ 3387] Fence Obstacle Course
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3387 [算法] f[i][0]表示从第i个栅栏的左端点走到原点的最少移动步数 f[i ...