Leetcode: 【每日一题】- 2019-09-02 - 回文

Created on 2 Sep 2019  ·  8Comments  ·  Source: azl397985856/leetcode

下面的问题,可以用小学三年级的方法解决,也可以使用初中方程式的方法解决,还可以使用大学的高级编程语言解决。请尝试使用所有方法解决,并进行比较。

有这样一个乘法算式:

人过大佛寺 * 我 = 寺佛大过人

这里的每一个字代表一个数字,不同的字代表不同的数字,你能把这些数字都找出来么?

<Beauty of Programming> Daily Question Easy stale

Most helpful comment

因为是五个不同的数字,其实循环次数可以再减少一些,就从12345-98765进行搜索

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    for( int i=12345; i<=98765; i++)
    {
        int a = i / 10000;
        int b = i % 10000 / 1000;
        int c = i % 10000 % 1000 / 100;
        int d = i % 10000 % 1000 % 100 / 10;
        int e = i % 10;
        if(a!=b && a!=c && a!=d && a!=e && b!=c && b!=d && b!=e && c!=d && c!=e && d!=e)
        {
            int j = e * 10000 + d * 1000 + c * 100 + b * 10 + a;
            if( j % i == 0 )
            {
            cout << i << "*" << (j/i) << "=" << j <<endl;
            }
                }
    }
    return 0;
}

All 8 comments

我这里给出一种“大人的”解法,思路就是从所有可能满足条件的数中暴力搜索即10000 - 99999,每一个数字我都去求它的回文数字, 并且观察每一位是不是否不同,如果都不同,我就用回文数除以原来的数字,得出的数字是整数(确切的说是一位整数)就将其返回。

先说答案, 答案是 21978 * 4 = 87912

为了方便理解,我这里做了两次循环,其实可以放到一起, 大神请忽略~

function reversedNumber(n) {
  let r = 0;

  while (n >>> 0 !== 0) {
    r = r * 10 + (n % 10);
    n = (n / 10) >>> 0;
  }

  return r;
}

function hasDuplicated(n) {
  const  used = {};

  while (n >>> 0 !== 0) {
    n = (n / 10) >>> 0;
    if (used[n % 10]) return true;
    used[n % 10] = true;
  }

  return false;
}
function t() {
  let n = 0;
  for (let i = 10000; i < 100000; i++) {
    if(hasDuplicated(i)) continue;
    n = reversedNumber(i);
    if (n / i === (n / i) >>> 0) return `${i} * ${n / i} = ${n}`;
  }
}

console.log(t()); // 21978 * 4 = 87912

public class Test {
    public static void main(String[] args) {
        for (int i = 10000; i < 99999; i++) {
            int a = i / 10000;
            int b = i / 1000 % 10;
            int c = i / 100 % 10;
            int d = i / 10 % 10;
            int e = i % 10;
            HashSet<Integer> set = new HashSet<Integer>();
            set.add(a);
            set.add(b);
            set.add(c);
            set.add(d);
            set.add(e);
            if (set.size() == 5) {
                for (int j = 1; j < 10; j++) {
                    if (i * j == (e * 10000 + d * 1000 + c * 100 + b * 10 + a)) {
                        System.out.println(a + "," + b + "," + c + "," + d + "," + e + "," + j);
                    }
                }
            }

        }
    }
}

因为是五个不同的数字,其实循环次数可以再减少一些,就从12345-98765进行搜索

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    for( int i=12345; i<=98765; i++)
    {
        int a = i / 10000;
        int b = i % 10000 / 1000;
        int c = i % 10000 % 1000 / 100;
        int d = i % 10000 % 1000 % 100 / 10;
        int e = i % 10;
        if(a!=b && a!=c && a!=d && a!=e && b!=c && b!=d && b!=e && c!=d && c!=e && d!=e)
        {
            int j = e * 10000 + d * 1000 + c * 100 + b * 10 + a;
            if( j % i == 0 )
            {
            cout << i << "*" << (j/i) << "=" << j <<endl;
            }
                }
    }
    return 0;
}

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

include

int main()
{
int a,b,c,d,e,f,i;
for(i=10000;i<=99999;i++)
for(f=1;f<=9;f++)
{
a=i%10;
b=i/10%10;
c=i/100%10;
d=i/1000%10;
e=i/10000;
if(if==a10000+b1000+c100+d*10+e&&f!=a&&f!=b&&f!=c&&f!=d&&f!=e)
if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e)
printf("%d %d\n",i,f);
}
}
结果是21978 *4=87912

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings