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 × ...
随机推荐
- Ajax json 数据格式
ajax : 是么是同步 什么事异步 同步现象:客户端发送请求到服务端,当服务端返回响应之前,客户端都处于等待卡死状态. 异步现象:客户端发送请求到服务器端,无论服务器是否返回,客户端都可以随意做其他 ...
- HDU 1599
裸的FLOYD 求最小环. #include <iostream> #include <cstdio> using namespace std; ; ; int n,m,min ...
- TensorFlow 便捷的实现机器学习 三
TensorFlow 便捷的实现机器学习 三 MNIST 卷积神经网络 Fly Overview Enabling Logging with TensorFlow Configuring a Vali ...
- 原型设计模式prototype-构造js自己定义对象
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 歌乐电子一道非常easy的笔试题目居然搞错了!!!
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjI0NzQ2Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- 自己定义UISlider的样式和滑块
//自己定义UISlider的样式和滑块 //轨道图片 UIImage *stetchLeftTrack = [UIImage imageNamed:@"thick"]; UIIm ...
- 程序中的文件之沙盒以及plist文件的初步使用
沙盒是相对于"应用程序"的文件,也就是相相应app所在的页面的文件. 每个应用都有自己的应用沙盒(应用沙盒就是文件系统文件夹).与其它文件系统隔离.应用必须呆在在积极的沙盒中.其它 ...
- jmeter函数和变量
函数和变量广泛的应用在JMeter的传参过程,其中函数可以被认为是某种特殊的变量,它们可以被采样器或者其他测试元件所引用. 常用函数 1.__RamdomString() / __Ramdom() 获 ...
- sql处理高并发
sql处理高并发,防止库存超卖 2014-08-14 23:44 13560人阅读 评论(2) 收藏 举报 分类: 数据库(43) 今天王总又给我们上了一课,其实mysql处理高并发,防止库存超卖 ...
- MFC 程序的运行流程
CWinApp::InitApplication CMyWinApp::InitInstance CMyFrameWnd::CMyFrameWnd CFrameWnd::Create CWnd::Cr ...