Project Euler__problem 4
来源:互联网 发布:sql server 2008 合计 编辑:程序博客网 时间:2024/06/06 19:12
Problem 4
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.
最大回文乘积
回文数就是从前往后和从后往前读都一样的数。由两个2位数相乘得到的最大回文乘积是 9009 = 91 × 99。
找出由两个3位数相乘得到的最大回文乘积。
做这题时遇到的问题主要是确定乘积的位数
后来不会做到三位数跟两位数放在同一个程序尝试的方法
那就自己手动缩小范围(说到底还是实力不足)
两位数的乘积可以是10*10~99*99 就是两种情况:3位数跟4位数
同理可知三位数的乘积也是两种情况:5位数跟6位数
这样,我写了一个简单的程序找出两位数的乘积的所有回文数
#include<iostream>int fun1(int a){int count = 1;while ((a / 10) != 0){a = a / 10;count++;}return count;}int evennum(int a){int units, tens, hundreds, kilo;units = a % 10;kilo = a / 1000;hundreds = (a / 100) - (kilo * 10);tens = (a / 10) - (a / 100 * 10);while (units == kilo&&tens == hundreds){ std::cout << a << "是一个回文数" << std::endl; break;}return a;}int oddnum(int a){int units, hundreds;units = a % 10;hundreds = a / 100;while (units == hundreds){ std::cout << a << "是一个回文数" << std::endl; break;}return a;}int main(){int num1, num2, product;int digit;int max;for (num1 = 99; num1 >= 10; num1--)for (num2 = 99; num2 >= 10; num2--){product = num1*num2;digit = fun1(product);if (digit % 2 == 0){evennum(product);}else{oddnum(product);}}system("pause");return 0;}
其中fun1是用来判断product的位数 even 跟odd那两个函数就是偶数跟质数的情况
这样轻易获得两位数乘积;但我发现,第一个出现的回文数,在二位数是9009最大
但在3位数却不一定 还要写一个比较的式子或者函数,并且我那个有太大局限性
后来想到可以直接写一个函数 将数字完全倒转过来的;后来就有了大改。
#include<iostream>int fun1(int a){int palindromic = 0;int origin = a;while (a){palindromic = a % 10 + palindromic * 10;a /= 10;}return palindromic == origin;}int main(){int num1, num2, product;int digit1, digit2;int max = 0;for (num1 = 99; num1 >= 10; num1--)for (num2 = 99; num2 >= 10; num2--){ product = num1*num2; if (fun1(product) == 1){if (product > max){max = product;digit1 = num1;digit2 = num2;}}}std::cout << max << "是最大回文数" << std::endl;std::cout << "且" << max << " = " << digit1 << "*" << digit2;system("pause");return 0;}
fun1那段是在论坛看到的,感觉到数学是美丽的
把数字改为999就可以得到结果906609=993*913
阅读全文
1 0
- Project Euler__problem 4
- Project Euler__problem 1
- Project Euler__problem 2
- Project Euler__problem 3
- Project Euler__problem 5
- Project Euler__problem 6
- Project Euler__problem 7
- Project Euler__problem 8
- Project Euler__problem 9
- Project Euler__problem 10
- Project Euler - Problem 4
- Project Euler problem 4
- Project Euler 4
- Project Euler 4
- Mini-project # 4 - "Pong"
- Project Euler - 4
- Mini-project #4 - "Pong"
- Project Euler Problem 4
- linux基础命令(六)文件权限
- CPU与GPU协同工作
- spoj highways 【生成树定理】
- Linux驱动编程——双通道LVDS屏幕配置
- Mybatis实战教程系列
- Project Euler__problem 4
- git多用户管理及权限控制
- 超级干货-融云消息与推送概念答疑
- [BZOJ]4821: [Sdoi2017]相关分析 线段树
- 面向对象和面向过程的区别和联系
- C#关键笔记整理
- 线段树求逆序数(hdu1394)
- POI导出下载
- 操作系统面试题目选汇(小的知识点)