鱼C论坛

 找回密码
 立即注册
查看: 25288|回复: 129

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

  [复制链接]
发表于 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
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-30 04:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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