找出两个三位数问题产品的最大回文 [英] Finding the largest palindrome of the product of two three digit numbers problem

查看:143
本文介绍了找出两个三位数问题产品的最大回文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,在项目欧拉的问题4 规定如下:

So on Project Euler the Problem 4 states the following:

一个回文数读取相同的   双向。取得最大的回文   由两个2位数字的产物   数字是9009 = 91 99。

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.

查找制造的最大的回文   两个3位数字的乘积。

Find the largest palindrome made from the product of two 3-digit numbers.

我已经试过如下:

    #include <stdio.h>
    #include <stdlib.h>

    int check(int result)
    {
        char b[7];
        sprintf(b, "%d", result);
        if (b[0] == b[5] && b[1] == b[4] && b[2] == b[3])
        {
            return 1;
        }
        else 
        {
            return 0;
        }
    }

    int main () {
        int i;
        int g;
        int final;
        for (i = 999; i > 99; i--)
        {
            for (g = 999; g > 99; g--)
            {
                if (check(g*i) == 1)
                {
                    final = g*i;
                    goto here;
                }
            }
        }
        here:
        printf("%d", final);
}

不过,这也不行。相反,正确的答案的,我得到的580085,我的猜测是回文最少,但仍然没有正确的答案。

But, this does not work. Instead of the right answer, I get 580085, which I guess is a palindrome at least, but still not the right answer.

让我解释一下我的程序从开始INT主

Let me explain my program starting from int main:

  1. INT I INT摹是我的乘数。他们是这两个三位数。
  2. INT最后是将存储最大的回文数。
  3. 在我开始两个for循环要下去把每一个号码的可能性。
  4. 在我走出循环使用GOTO当第一个回文达到(可能不应该,但是,这并不影响一个小程序,类似这样的太多了)。
  5. 在第一个回文应该是最大的一个可能的,因为我是从上向下计数。
  1. int i and int g are my multipliers. They are those two three digit numbers.
  2. int final is the number that will store the largest palindrome.
  3. I start two for loops going to down to get every number possibility.
  4. I get out of the loop using a goto when the first palindrome is reached(probably should not but, it doesn't effect a small program like this too much).
  5. The first palindrome should be the biggest one possible since I am counting down from the top.

现在让我解释一下我的检查:

Let me now explain my check:

  1. 首先,因为这些是相乘的大小来确定二三一位数字的字符将需要持有该值我去了一个计算器,再乘以999 * 999,并结束了6,然后我需要添加因为我发现从一个我张贴的问题早前的sprintf 将一个 \ 0 字符结束。
  2. 好了,现在我有一个字符和一切,我抄结果(其中我* G INT主),并把它放在字符B〔7〕
  3. 然后我只是检查 B ,看它是否等于它的自我与硬编码,我需要检查每个插槽。
  4. 然后我回因此,1表示是; 2假的。
  1. First off since these are two three digit numbers multiplying together to determine the size a char would need to be to hold that value I went to a calculator and multiplied 999 * 999 and it ended up being 6 then I need to add one because I found out from one the questions I posted earlier that sprintf puts a \0 character at the end.
  2. Ok, now that I have a char and all, I copied result (which i*g in int main) and put it in char b[7].
  3. Then I just checked b to see if it equalled it self with by hard coding each slot I needed to check for.
  4. Then I returned accordingly, 1 for true, and 2 for false.

这似乎是完全合乎逻辑的我,但,它不会为一些奇怪的原因,工作。任何提示?

This seems perfectly logical to me but, it does not work for some weird reason. Any hints?

推荐答案

这个假设是错误的:

第一个回文应该是最大的一个可能的,因为我是从上向下计数。

The first palindrome should be the biggest one possible since I am counting down from the top.

您将检查 999 * 100 = 99900 998 * 101 = 100798 ,让您明明白白不容依靠这一点。

You will check 999*100 = 99900 before 998*101 = 100798, so clearly you can´t count on that.

这篇关于找出两个三位数问题产品的最大回文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆