鱼C论坛

 找回密码
 立即注册
楼主: 欧拉计划

题目4:找出由两个三位数乘积构成的回文

  [复制链接]
发表于 2020-8-10 22:34:54 | 显示全部楼层
KingFish-v- 发表于 2020-8-10 21:44
答案是993*913=906609,用时0.01s左右。

找4位数x4位数的最大回文用时也在0.01s内,5位数x5位数用时0.58 ...

不错,我也学习一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-21 16:37:59 | 显示全部楼层
  1. a = 1000
  2. b = 1000
  3. l1 = []
  4. while a > 99:
  5.     a -= 1
  6.     b = 1000
  7.     while b >99:
  8.         b -= 1
  9.         s = a*b
  10.         c = list(str(s))
  11.         i = len(c)
  12.         x = 1
  13.         while x <= int(i/2) :
  14.             if c[x-1] != c[0-x]:
  15.                 break
  16.             if x == int(i/2):
  17.                 l1.append(s)
  18.                 break
  19.             x += 1
  20. print(max(l1))
复制代码


基本解法,忙完回头看~~~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-1 17:19:43 | 显示全部楼层
# 一个回文数指的是从左向右和从右向左读都一样的数字。最大的由两个两位数乘积构成的回文数是 9009 = 91 * 99。
# 找出最大的有由个三位数乘积构成的回文数。
def huiwen(x):
    y = str(x)[::-1]
    if int(y) == x:
        return True
    else:
        return False

max_Num = 0
for i in range(100, 999):
    for j in range(100, 999):
        x = j * i
        if huiwen(x):
            if max_Num < x:
                max_Num = x

print(max_Num)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-2 12:16:19 | 显示全部楼层
  1. '''一个回文数指的是从左向右和从右向左读都一样的数字。最大的由两个两位数乘积构成的回文数是 9009 = 91 * 99。
  2. 找出最大的由两个三位数的乘积构成的回文数。'''

  3. palindrome = []

  4. def ispalindrome(num):
  5.     s = str(num)
  6.     a = 0
  7.     b = len(s)-1
  8.     check = 0
  9.     for i in range(int((len(s)/2))):
  10.         if len(s) % 2 != 0:
  11.             return False
  12.         if s[a] != s[b]:
  13.             if check == 0:
  14.                 check += 1
  15.             if check != 0:
  16.                 break
  17.         a += 1
  18.         b -= 1
  19.     if check == 0:
  20.         return True

  21. def multiply(num1,num2):
  22.     for number1 in range(num1, 99, -1):
  23.         for number2 in range(num2, 99, -1):
  24.             number = number1*number2
  25.             if ispalindrome(number):
  26.                 palindrome.append([number1, number2, number])

  27. def getbiggestpalindrome():
  28.     temp = 0
  29.     re = 0
  30.     for i in range(len(palindrome)-1):
  31.         if temp < palindrome[i][2]:
  32.             temp = palindrome[i][2]
  33.             re = i
  34.     print(palindrome[re])

  35. start_palindrome = time.time()
  36. multiply(999,999)
  37. getbiggestpalindrome()
  38. time_palindrome = time.time() - start_palindrome
  39. print("%f秒" %time_palindrome)
复制代码



[993, 913, 906609]
0.712786秒
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-4 22:29:43 | 显示全部楼层
def palin(x):
    if str(x) == str(x)[::-1]:
        return True
    return False

def getp():
    list1 = [0]
    list2 = [0,0]
    for i in range(100,1000):
        for j in range (100,1000):
            if palin(i*j):
                x = i * j
                if x > list1[0]:
                    list1.pop()
                    list1.append(x)
                    if (i + j )> (list2[0] + list2[1]):
                        list2.pop()
                        list2.pop()
                        list2.append(i)
                        list2.append(j)

    print (list2)
    print (list1)
getp()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-26 23:19:49 | 显示全部楼层
  1. #include <stdio.h>

  2. #define NUM 1000

  3. int main ()
  4. {
  5.         int max_1 = -1024, max_2 = 0, max_3 = 0;
  6.         int multiply, i, j;
  7.         int a, b, c, d, e, f;
  8.         for (i = 900; i < NUM; i++)
  9.         {
  10.                 for (j = 900; j < i; j++)
  11.                 {
  12.                         multiply = i * j;
  13.                         a = multiply / 100000 % 10;
  14.                         b = multiply / 10000 % 10;
  15.                         c = multiply / 1000 % 10;
  16.                         d = multiply / 100 % 10;
  17.                         e = multiply / 10 % 10;
  18.                         f = multiply % 10;
  19.                        
  20.                         if (a == f && b == e && c == d)
  21.                         {
  22.                                 if (multiply > max_1)
  23.                                 {
  24.                                         max_1 = multiply;
  25.                                         max_2 = j;
  26.                                         max_3 = i;
  27.                                 }
  28.                         //        printf("%d = %d * %d\n", multiply, i, j);
  29.                         }
  30.                 }
  31.         }
  32.         printf("%d = %d * %d\n", max_1, max_2, max_3);
  33.        
  34.         return 0;
  35. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-6 13:23:21 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <math.h>



  3. main()
  4. {
  5.         int i, j, k, num, mun = 0, temp;

  6.         for (i = 998001; i > 10000; i--)
  7.         {
  8.                 num = i;
  9.                 for (j = 5; j >= 0; j--)
  10.                 {
  11.                         temp = num % 10;
  12.                         mun += (temp * pow(10, j));
  13.                         num = (num - temp) / 10;
  14.                 }
  15.                 if (mun == i)
  16.                 {
  17.                         for (k = 999; k > 100; k--)
  18.                         {
  19.                                 if (mun % k == 0)
  20.                                 {
  21.                                         temp = mun / k;
  22.                                         if ((temp / 1000) == 0)
  23.                                         {
  24.                                                 goto Label;
  25.                                         }
  26.                                 }
  27.                         }
  28.                 }
  29.                 mun = 0;
  30.         }
  31.         Label:printf("%d = %d * %d\n", mun, k, mun / k);
  32.        
  33. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-10 22:45:29 From FishC Mobile | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2021-3-10 22:50 编辑
  1. constexpr bool isPalindrome(unsigned int val)noexcept {
  2.     unsigned int copy = val, rev = 0;

  3.     while (val) {
  4.         rev += val;
  5.         val /= 10;
  6.         rev -= val * 10;
  7.     }

  8.     return !(copy ^ rev);
  9. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-1 21:11:54 | 显示全部楼层
无名侠 发表于 2015-7-9 14:41
难点应该就是判断是否为回文了。
我用的方法有些歪门邪道,利用字符串来判断,不过效率还行。
然后就是三 ...

906609
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-2 08:56:15 | 显示全部楼层
906609
  1.    public static void main(String[] args) {
  2.         System.out.println(largestPalindrome(3));

  3.     }

  4.     public static int largestPalindrome(int n) {
  5.         if(n == 1) return 9;
  6.         //计算给定位数的最大值
  7.         long max = (long)Math.pow(10,n) - 1;
  8.         //从max - 1开始循环,原因见上文
  9.         for(long i = max - 1; i > max / 10; i--){
  10.             //1. 构造回文数
  11.             String s1 = String.valueOf(i);
  12.             long rev = Long.parseLong(s1 + new StringBuilder(s1).reverse().toString());
  13.             //2. 检验该回文数能否由给定的数相乘得到
  14.             for(long x = max; x * x >= rev; x --){
  15.                 if(rev % x == 0) return (int)(rev );
  16.             }
  17.         }
  18.         return -1;
  19.     }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-26 14:20:34 | 显示全部楼层
  1. def number(x):
  2.     y = 10**x-1
  3.     z = 10**(x-1)
  4.     for i in range(y**2,z**2,-1):
  5.         j = int(str(i)[::-1])
  6.         if i == j:
  7.             for m in range(y,z,-1):
  8.                 if i% m == 0:
  9.                     n = int(i/ m)
  10.                     if n in range(z,y):
  11.                         return m, n, i
  12. print(number(3))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-3 20:51:40 | 显示全部楼层
  1. #由于909*902=819918,我们从这里开始。
  2. target=[]
  3. for a in range(909,1000):
  4.     for b in range(902,1000):
  5.         r=a*b#r for result
  6.         #显然所求的数为六位数

  7.         if (str(r)[0]==str(r)[5])and (str(r)[1]==str(r)[4]) and (str(r)[2]==str(r)[3]):
  8.             target.append(r)
  9. print(target)
复制代码

结果为:
[819918, 906609, 824428, 886688, 861168, 888888, 861168, 888888, 886688, 906609]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-6 16:53:12 | 显示全部楼层
本帖最后由 michaelwatson 于 2021-5-6 16:55 编辑

    好啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-18 15:42:52 | 显示全部楼层
a = []

for m in range(99,1000):
    for n in range(99,1000):
        i = m*n
        b = str(i)
        c = list(b)
        if c[::-1] == c:
            a.append(i)
        
print(max(a))
答案906609
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-15 19:53:36 | 显示全部楼层
a = 0
b = ''
for i in range(100,1000):
      for o in range(100,1000):
            if str(i*o) == str(i*o)[::-1]:
                  if i*o > a:
                        a = i*o
                        b = "(" + str(i) + ',' + str(o) + ')'
                  else:
                        continue
            else:
                  continue
else:
      print(a)
      print(b)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-8 22:52:51 | 显示全部楼层
  1. #include <stdio.h>

  2. int count;

  3. int ispalindrome(int n)
  4. {
  5.         int result = 0;
  6.         int value = n;
  7.         while(n!=0)
  8.         {
  9.                 count++;
  10.                 result = (n%10) + result * 10;
  11.                 n = n / 10;
  12.         }
  13.         return (result == value)?1:0;
  14. }

  15. int main(void)
  16. {
  17.         int max_i, max_j;
  18.         int max;
  19.        
  20.         int i=999,j=999;

  21.         for(i=999;i>=100;i--)
  22.         {
  23.                 for(j=999;j>=100;j--)
  24.                 {
  25.                         if (i<max_i && j < max_j) break; //不加这段count = 4718843加上count = 500591
  26.                         if (ispalindrome(i * j)){
  27.                                 if (i * j > max){
  28.                                         max_i = i;
  29.                                         max_j = j;
  30.                                         max = (i * j > max)?i * j:max;
  31.                                 }
  32.                         }
  33.                 }
  34.         }
  35.         printf("%d * %d = %d\n", max_i, max_j, max);
  36.         printf("count = %d\n", count);
  37.         return 0;
  38. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-6 09:15:32 | 显示全部楼层
#include <iostream>
using namespace std;
int main()
{
                int a,b,c;
                for(int i=100;i<=999;i++)
                {
                        a=i%10;
                        b=i/10%10;
                        c=i/100;
                        if(a==c)
                                cout<<i<<endl;
                }
                return 0;
}
//C++写的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-8 16:18:07 | 显示全部楼层
#最大的两个三位数乘积构成的回文数

#检查是否回文
def check(n, start, end):
    if start > end:
        return 1     
    else:
        return check(n, start+1, end-1) if n[start] == n[end] else 0

#提取数字计算
result = []
res = []
for a in range(100, 999):
    for b in range(500,999):
        string = str(a * b)
        length = len(string) - 1
        if check(string, 0, length):
            result.append(int(string))
            if max(result) == int(string):
                res.append((a,b))

print(res[-1])

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-27 13:42:09 | 显示全部楼层
本帖最后由 番杰 于 2021-10-27 13:58 编辑
  1. #include<stdio.h>

  2. int mian(void)
  3. {
  4.         int t1,t10,t100,temp;   
  5.         int a,b,max;
  6.         int flag = 0 ;
  7.         for(int i = 997799;i>10000;i--)  //  从997799开始,是因为它是998001(999*999)之下最大的回文数
  8.         {
  9.                 if(i>100000)  //六位回文数
  10.                 {
  11.                         t1 = i % 10;
  12.                         t10 = (i % 100) / 10;
  13.                         t100 = (i % 1000) /100;
  14.                         temp = (t1 * 100) + (t10 * 10) + t100;       
  15.                        
  16.                         if(temp == (i/1000))   //判断是否是回文数
  17.                         {
  18.                                 for(int j = 999;j>=100;j--)
  19.                                 {
  20.                                         if(i % j == 0)
  21.                                         {
  22.                                                 if(j > 99 && j < 1000)
  23.                                                 {
  24.                                                         a = j ;
  25.                                                         b = i / j ;
  26.                                                         max = i;
  27.                                                         flag = 1;
  28.                                                         break;
  29.                                                 }
  30.                                         }
  31.                                 }
  32.                                 if(flag == 1)
  33.                                         break;       
  34.                         }
  35.                 }
  36.                 else   //五位回文数
  37.                 {
  38.                         t1 = i % 10;
  39.                         t10 = (i % 100) / 10;
  40.                         temp = (t1 * 100) + (t10 * 10) ;       
  41.                        
  42.                         if(temp == (i/1000))
  43.                         {
  44.                                 for(int j = 999;j>=100;j--)
  45.                                 {
  46.                                         if(i % j == 0)
  47.                                         {
  48.                                                 if(j > 99 && j < 1000)
  49.                                                 {
  50.                                                         a = j ;
  51.                                                         b = i / j ;
  52.                                                         max = i;
  53.                                                         flag = 1;
  54.                                                         break;
  55.                                                 }
  56.                                         }
  57.                                 }
  58.                                 if(flag == 1)
  59.                                         break;       
  60.                 }
  61.         }

  62.         printf("%d = %d + %d",c,a,b);

  63.         return 0 ;
  64. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-13 02:10:38 | 显示全部楼层
最大回文数为:993 * 913 = 906609
  1. #include<stdio.h>

  2. int main(void)
  3. {
  4.         int i, j;
  5.         int mul, lum, max = 0;
  6.         int k, buf;
  7.         int a, b;
  8.        
  9.         for (i = 999; i > 100; i--)
  10.         {
  11.                 for (j = 999; j > 100; j--)
  12.                 {
  13.                         lum = 0;
  14.                         mul = i * j;
  15.                         buf = mul;
  16.                         while (mul)
  17.                         {
  18.                                 k = mul % 10;
  19.                                 mul = mul / 10;
  20.                                 lum = lum * 10 + k;
  21.                         }
  22.                         if (lum == buf)
  23.                         {
  24.                                 if (lum > max)
  25.                                 {
  26.                                         max = lum;
  27.                                         a = i; b = j;
  28.                                 }
  29.                         }
  30.                 }
  31.         }
  32.        
  33.         printf("最大回文数为:%d * %d = %d\n", a, b, max);
  34.        
  35.         return 0;
  36. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-19 07:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表