Problem4-Project Euler
Largest palindrome product
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
寻找能够分解为两个三位数乘积的回文数:
先分析,回文数范围在:100^2=10000到999^2=998001之间,暴力也可行……但适当剪枝会快一些
考虑100000及以上时,回文数能被11整除,在循环中就可以抛去其它部分数,解答会快一些。
还想用用时间换空间可以先打个质数表……之类的
#include"stdio.h"
#include"stdlib.h"
#include"string.h" int palindrome(int a,int b) /*palindrome function:to test if a product is palindrome*/
{
int sum,i,j;
char num[];
sum=a*b;
itoa(sum,num,);
for(i=,j=strlen(num)-;i<j;i++,j--)
if(num[i]!=num[j])
return();
return();
} int main() /*problem4-Largest palindrome product*/
{
int i,j,max=,m1=,m2=;
for(i=;i>=;i--)
for(j=i;j>=;j--)
{
if(i%!=&&j%!=) //剪枝,i,j之中必有一个为11倍数
continue;
if(palindrome(i,j)==&&i*j>max)
{
max=i*j;
m1=i;
m2=j;
}
}
printf("max palindrome is %d=%d*%d\n",max,m1,m2);
return();
}
Problem4-Project Euler的更多相关文章
- [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 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 ...
- 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 × ...
- Project Euler 第一题效率分析
Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...
- Python练习题 049:Project Euler 022:姓名分值
本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...
- Python练习题 048:Project Euler 021:10000以内所有亲和数之和
本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...
- Python练习题 047:Project Euler 020:阶乘结果各数字之和
本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...
随机推荐
- 【从0到1学jQuery】jQuery中each()和$.each()的使用
引子: 最近遇到一个问题,就是在each()函数中怎么模拟for循环中的break和continue的操作.所以就查看了jQuery关于这个函数的文档,并且总结一下. 演示代码如下: <div& ...
- JavaScript初探四
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- mysql delimiter的说明
默认情况下,mysql解释器一遇到分号(;),它就要自动执行. 不会等到用户把这些语句全部输入完之后,再执行整段语句. 而自定义函数和存储过程的SQL语句有好多行,且语句中包含有分号,为了保证整段语句 ...
- linux比较文件夹的差异命令
可以使用 diff -ruNa s1 s2 或者使用 diff -uN c1 c2 结果如下: sandbox$ tree . |-- dir1 | |-- a.txt | `-- b.txt `-- ...
- 各大APP注册时发送短信验证码是怎么实现的?
第一步:获得验证码:1.找到相关的表.2.用什么发送,post,get ,ajax,当然ajax首选3.post之前要js先判断是手机号码11位,并且全部都是数字,或者用正则也行.4.用ajax发送数 ...
- Python常见问题
1. 处理中文时出现的错误'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)" 解决方 ...
- [机器学习] 性能评估指标(精确率、召回率、ROC、AUC)
混淆矩阵 介绍这些概念之前先来介绍一个概念:混淆矩阵(confusion matrix).对于 k 元分类,其实它就是一个k x k的表格,用来记录分类器的预测结果.对于常见的二元分类,它的混淆矩阵是 ...
- zoj 2724 Windows Message Queue(使用priority_queue容器模拟消息队列)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2724 题目描述: Message queue is the b ...
- .5-浅析webpack源码之入口函数
从convert-argv出来后,目前进度在这: yargs.parse(process.argv.slice(2), (err, argv, output) => { // ... // 从这 ...
- T-SQL建索引
USE database GO ------------开始----------- ALTER TABLE [name] DROP CONSTRAINT 主键约束 ----删除主键约束 IF ...