鱼C论坛

 找回密码
 立即注册
查看: 11386|回复: 127

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

  [复制链接]
发表于 2015-4-20 23:41:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 欧拉计划 于 2017-1-14 17:43 编辑
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.


题目:

一个回文数指的是从左向右和从右向左读都一样的数字。最大的由两个两位数乘积构成的回文数是 9009 = 91 * 99。

找出最大的有由个三位数乘积构成的回文数。


评分

参与人数 2贡献 +2 收起 理由
xg-sco + 1 哈哈,我做出来了,无条件支持楼主!
cwhsmile + 1 (906609, 993, 913)用时0.15s

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-6-29 19:49:58 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-6-30 18:05 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. int main(void)
  5. {
  6.     int a,b,c,i,p,q,max=0,x,y;
  7.     char num[10];
  8.     for(a=999;a!=100;a--)
  9.     {
  10.       for(b=999;b!=100;b--)
  11.         {
  12.             c = a * b;
  13.             itoa (c,num,10);
  14.             p = strlen(num);
  15.             q = p/2;
  16.             for(i=0;i<q;i++)
  17.             {
  18.             if(num[i] != num[p-i-1])
  19.             {
  20.             goto z;
  21.             }
  22.             }
  23.             if(c > max)
  24.             {
  25.             max = c;
  26.             x = a;
  27.             y = b;
  28.             }
  29. z:         c = 0;
  30.        }
  31.     }
  32.     printf("%d * %d=%d\n",x,y,max);

  33. }
复制代码


如果有错误希望指出
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-7-9 14:41:46 | 显示全部楼层
难点应该就是判断是否为回文了。
我用的方法有些歪门邪道,利用字符串来判断,不过效率还行。
然后就是三位数的枚举。
A * B = B * A
所以我用了两层循环。 第一层循环从500向下递减,第二层循环是从1000向下递减。
从最大向下递减的好处:找到的第一个回文数就是最大的:lol:

输出结果:
499 * 737 =  367763
  1. def deDec(x):
  2.         str1 = str(x)
  3.         str3 = ""
  4.         x2 = len(str1)
  5.         x2-=1
  6.         while x2>=0:
  7.                 str3=str3+str1[x2]
  8.                 x2-=1
  9.         return str3==str1

  10. def pdr():
  11.         x1=500
  12.         x2=1000
  13.         while x1>0:
  14.                 while x2>=500:
  15.                         y=x1*x2
  16.                         if deDec(y):
  17.                                 print(str(x1)+" * "+str(x2)+" = ",y)
  18.                                 return
  19.                         x2-=1
  20.                 x2=1000
  21.                 x1-=1
复制代码

如果有错误,还望各位大神们指出。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 2

使用道具 举报

发表于 2015-7-19 22:07:14 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<windows.h>
  3. int main()
  4. {
  5.         int i, j = 0,k,ys;
  6.         for(i = 998001;i>10000;--i) /*从最大的三位数乘积999*999的值998001--最小的100*100的值10000开始遍历*/
  7.         {
  8.                 k = i;
  9.                 while(k != 0) /*将i倒置*/
  10.                 {
  11.                         ys = k % 10;
  12.                         k = k / 10;
  13.                         j = j * 10 + ys;
  14.                 }
  15.                 if(i == j) /*判断i是否为回文数*/
  16.                 {
  17.                         for(k = 100;k<999;++k) /*判断该数是否能由两三位数相乘得到 将该数除以一三位数(100-999)*/
  18.                         {
  19.                                 if(i%k==0) /*判断该数是否能由一三位数于其他数相乘得到*/
  20.                                 {
  21.                                         if(i/k >= 100 && i/k <1000) /*判断该数是否为三位数*/
  22.                                         {
  23.                                                 goto s;
  24.                                         }
  25.                                         else if(i/k<100) /*若以为二位数说明已不符合退出循环*/
  26.                                         {
  27.                                                 break;
  28.                                         }
  29.                                         else /*若为四位数继续加大k的值继续循环不做任何动作*/
  30.                                         {
  31.                                                 ;
  32.                                         }
  33.                                 }
  34.                         }
  35.                 }
  36.                 else
  37.                 {
  38.                         j = 0;
  39.                 }
  40.         }
  41. s:printf("The best is:%d = %d * %d\n",j,k,j/k);
  42.   system("pause");
  43.   return 0;
  44. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

发表于 2015-10-8 17:38:53 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-6-30 18:06 编辑
  1. def int_reverse(x):
  2.     '求一个数的反转:1234得到4321'
  3.     x1 = str(x)
  4.     x1 = list(x1)
  5.     x1.reverse()
  6.     x1 = ''.join(x1)
  7.     x1 = int(x1)
  8.     return x1
  9.         
  10. def last_pal(digt):
  11.    
  12.     '两个digt位数乘积得到的最大回文'
  13.    
  14.     x = range(10**(digt-1),10**digt)
  15.     y = range(10**(digt-1),10**digt)
  16.     pal = {}
  17.     for i in x:
  18.         for j in y:
  19.             s = i * j
  20.             s1 = int_reverse(s)
  21.             if s == s1:
  22.                 pal[s] = (i,j)
  23.             
  24.     last_pal = max(pal.keys())
  25.     print(last_pal)        
  26.     print(pal[last_pal])

  27. last_pal(3)
复制代码

==================================
结果:906609  (993, 913)
但是感觉循环的使用,是的程序比较慢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-11-2 14:06:42 | 显示全部楼层
  1. /*题目4:找出由两个三位数乘积构成的回文*/

  2. #include <iostream>
  3. using namespace std;

  4. int main()
  5. {
  6.         int a; //乘积
  7.         int max = 0;//最大回文数
  8.        
  9.         int i,j;//两个三位数

  10.         for (i = 100;i <= 999;i ++)
  11.         {
  12.                 for (j = 100;j <= 999;j ++)
  13.                 {
  14.                         a = i * j;
  15.                        
  16.                         if (a / 100000 > 0) //判断a是否六位数
  17.                         {
  18.                                 if (a / 100000 == a%10 && \
  19.                                         (a % 100000) / 10000 == (a % 100) / 10 && \
  20.                                         (a % 10000) / 1000 == (a % 1000) / 100)
  21.                                 {
  22.                                         cout<<"回文数为:"<<a<<endl;

  23.                                         if (a > max)
  24.                                         {
  25.                                                 max = a;
  26.                                         }
  27.                                 }
  28.                                        
  29.                         }

  30.                         else
  31.                         {
  32.                                 if (a / 10000 == a % 10 && \
  33.                                         (a % 10000) / 1000 == (a % 100) / 10)
  34.                                 {
  35.                                        
  36.                                         cout<<"回文数为:"<<a<<endl;

  37.                                         if (a > max)
  38.                                         {
  39.                                                 max = a;
  40.                                         }
  41.                                 }
  42.                         }

  43.                 }
  44.         }

  45.         cout<<"最大回文数为"<<max<<endl;

  46.         return 0;
  47. }
复制代码


输出:最大回文数为906609
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-3-16 17:42:43 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<time.h>
  3. int ispalindrome(int num)//仅对于这道题
  4. {
  5.         int a[6];
  6.         for (int i=0;i<6;i++)
  7.         {
  8.                 a[i]=num%10;
  9.                 num/=10;
  10.         }
  11.         if((a[0]==a[5])&&(a[1]==a[4])&&(a[2]==a[3]))
  12.                 return 1;
  13.         else
  14.                 return 0;
  15. }

  16. int checkfactor(int num)
  17. {
  18.         int i=100;
  19.         for(i;i<1000;i++)
  20.         {
  21.                 if(num%i==0)
  22.                 {

  23.                         if(((num/i)>=100)&&((num/i)<1000))
  24.                         return i;
  25.                 }
  26.         }
  27.         return 0;
  28. }

  29. int main()
  30. {
  31.         int i=997800;
  32.         int factor1,factor2;
  33.         double start, finish;
  34.         start = clock();//取开始时间
  35.         for(i;i>10000;i--)
  36.         {
  37.                 if((ispalindrome(i))&&(checkfactor(i)))
  38.                         break;
  39.         }
  40.         finish = clock();//取结束时间
  41.         printf( "\n%f seconds\n",(finish - start) / CLOCKS_PER_SEC);//以秒为单位显示之
  42.         factor1=checkfactor(i);
  43.         factor2=i/factor1;
  44.         printf("%d=%d*%d",i,factor1,factor2);
  45.         getchar();
  46. }

  47. 0.011s
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2016-5-1 23:40:54 | 显示全部楼层
  1. # -*- coding: utf-8 -*-
  2. """
  3. Palindromic number.
  4. Find lagest palindrome made from the prod of two 3-digit numbers.
  5. """

  6. def is_palindrome(num):
  7.     if str(num) == str(num)[::-1]:
  8.         return True
  9.     return False

  10. def find_palindrome(min_num=100, max_num=999):
  11.     for m in reversed(range(min_num, max_num + 1)):
  12.         for n in reversed(range(min_num, max_num + 1)):
  13.             if is_palindrome(m*n):
  14.                 yield m*n

  15. print(max(find_palindrome()))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-5-4 22:43:37 | 显示全部楼层
  1. list1 = []
  2. for i in range(100,1000):
  3.     for j in range(100, 1000):
  4.         ret = i*j
  5.         str_ret = list(str(ret))
  6.         str_ret2 = str_ret[:]
  7.         str_ret2.reverse()
  8.         if str_ret == str_ret2:
  9.             list1.append(ret)
  10. print(max(list1))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2016-6-10 18:29:01 | 显示全部楼层
  1. def a():
  2.         n = 999
  3.         while n>100:  #6位回文数
  4.                 m = n*1000+n%10*100+n//10%10*10+n//100
  5.                 for i in range(999,99,-1):
  6.                         j = m//i
  7.                         if m%i==0 and j>=100 and j<=999:
  8.                                 return(m,i,j)
  9.                 n-=1
  10.         n = 999
  11.         while n>100:  #5位的回文数
  12.                 m = n*100+n//10%10*10+n//100
  13.                 for i in range(999,99,-1):
  14.                         j = m//i
  15.                         if m%i==0 and j>=100 and j<=999:
  16.                                 return(m,i,j)
  17.                 n-=1
  18.                
  19. print(a())
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-6-13 14:06:35 | 显示全部楼层
  1. xx=0
  2. for i in range(100,1000):
  3.     for j in range(i,1000):
  4.         x=i*j
  5.         if x>xx:
  6.             list1=list(str(x))
  7.             l=len(list1)
  8.             p=1
  9.             for z in range((l+1)//2):
  10.                 if list1[z] != list1.pop():
  11.                     p=0
  12.                     break
  13.             if p:
  14.                 xx=x
  15.                 a,b=i,j
  16. print(xx)
复制代码


答案906609=913*993
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2016-6-14 23:56:12 | 显示全部楼层
想了下,没测试,是否可以设置,先算出乘积,字符串中s=“123456”,那么s[::-1]就==”654321”,直接这样判断,很多有reverse方法,或者reversed函数~不知对否
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-7-4 18:32:06 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-6-30 18:06 编辑
  1. >>> c=0
  2. >>> for i in range(999,100,-1):
  3.         for j in range(999,100,-1):
  4.                 a=str(i*j)
  5.                 b=a[::-1]
  6.                 if b==a:
  7.                         if c<int(a):
  8.                                 c=int(a)
  9.                                 d=[i,j]


  10. >>> print('最大的回文数是',c,'乘数',d[0],'*',d[1])
复制代码




为什么我发不错跟楼上一样的格式。。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-7-5 16:00:57 | 显示全部楼层
  1. def palindrome(n):
  2.     a = []
  3.     b = []
  4.     while(n):
  5.         result = n % 10
  6.         a.append(result)
  7.         n //= 10
  8.     b = a.copy()
  9.     b.reverse()
  10.     if b == a:
  11.         return 1
  12.     else:
  13.         return 0

  14. number = []

  15. for x in range(100, 1000):
  16.     for y in range(100, 1000):
  17.         i = x * y
  18.         num = palindrome(i)
  19.         if num:
  20.             number.append(i)

  21. print(max(number))
复制代码


906609

  1. #include <stdio.h>
  2. int palindrome(int x)
  3. {
  4.     int num = 0,temp = x;
  5.     while(x)
  6.     {
  7.        num = x % 10 + num *10;
  8.        x /= 10;
  9.     }
  10.     if (num == temp)
  11.         return 1;
  12.     else
  13.         return 0;
  14. }

  15. int max(int a[1000])
  16. {
  17.     int n = 0,temp;
  18.     while(a)
  19.     {
  20.         if(a[n] > a[n + 1])
  21.         {
  22.             a[n + 1] = a[n];
  23.         }
  24.         n++;
  25.     }
  26.     return a[n];
  27. }

  28. int main()
  29. {
  30.     int x, y, z, n = 0;
  31.     int a[1000];
  32.     for(x = 100; x <= 999; x++)
  33.     {
  34.         for(y = 100; y <= 999; y++)
  35.         {
  36.             z = x * y;
  37.             if(palindrome(z))
  38.             {
  39.                 a[n] = z;
  40.                 n++;
  41.             }
  42.         }
  43.     }
  44.     printf("%d",max(a));
  45.     return 0;
  46. }
复制代码

c的代码运行程序总是停止运行,有没有大神指点一下吖
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-7-5 16:04:00 | 显示全部楼层
冷钟天 发表于 2016-7-4 18:32
>>> c=0
>>> for i in range(999,100,-1):
        for j in range(999,100,-1):

表情左边有个“<>”样子的东西,这是专门发代码的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-7-5 17:28:46 | 显示全部楼层
幻世伽蓝 发表于 2016-7-5 16:04
表情左边有个“”样子的东西,这是专门发代码的

好的,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-7-26 18:39:14 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-6-30 18:06 编辑
  1. def palin(n):
  2.     if 100000<n<1000000:
  3.         result=str(n)
  4.         if result[:3]==result[5]+result[4]+result[3]:
  5.             return True
  6.         else:
  7.             return -1
  8.     else:
  9.         print('输入错误')
  10.         return -1
  11. Sum=[]   
  12. for i in range(500,1000):
  13.     for j in range(500,1000):
  14.         if palin(i*j) is True:
  15.             Sum.append(i*j)

  16. Sum.sort()
  17. Sum.reverse()
  18. print(Sum[0])
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-12 02:42:35 | 显示全部楼层
  1. def is_palin(s):
  2.     n = len(s)
  3.     i = 0
  4.     while i < n / 2:
  5.         if s[i] != s[-(i + 1)]:
  6.             return False
  7.         i += 1
  8.     return True


  9. def findpalin(m, n):
  10.     x = n * n
  11.     y = (m - 1) * (m - 1)
  12.     while y > x:
  13.         if is_palin(str(y)):
  14.             i = n
  15.             while i < m:
  16.                 if y % i == 0 and y / i >= n and y / i < m:
  17.                     return i, int(y / i), y
  18.                 i += 1
  19.         y -= 1
  20.     return None
  21. print(findpalin(1000, 100))
复制代码

(913, 993, 906609)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-26 12:45:13 | 显示全部楼层
  1. def ishuiwen(m):
  2.     a = list(str(m))
  3.     b = a.copy()
  4.     a.reverse()
  5.     if a == b:
  6.         return True
  7.     else:
  8.         return False

  9. result = []
  10. for i in range(999,0,-1):
  11.     for j in range(999,i-1,-1):
  12.         k = i*j
  13.         if ishuiwen(k):
  14.             result.append(k)
  15.             break
  16. max(result)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-28 10:18:01 | 显示全部楼层
  1. public class LargestPalindromeProduct
  2. {
  3.         // 判断整数n是不是回文数
  4.         public static boolean isPalindrome(int n)
  5.         {
  6.                 String s = String.valueOf(n);
  7.                 return new StringBuffer(s).reverse().toString().equals(s);
  8.         }
  9.         
  10.         public static void main(String[] args)
  11.         {
  12.                 int num = 0;   //保存回文数
  13.                 int x = 0;           //保存构成回文数的两个三位数因子
  14.                 int y = 0;
  15.                 for(int i = 999;i >= 100;i--)
  16.                 {
  17.                         for(int j = 999;j >= 100;j--)
  18.                         {
  19.                                 if(isPalindrome(i*j) && (i*j) > num)
  20.                                 {
  21.                                         num = i * j;
  22.                                         x = i;
  23.                                         y = j;
  24.                                 }                        
  25.                         }
  26.                 }
  27.                
  28.                 System.out.println("两个三位数的乘积得到的回文数是:" + num + " = " + x + " * " + y);
  29.         }
  30. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 09:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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