Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【每日一题】- 2019-09-02 - 回文 #158

Open
azl397985856 opened this issue Sep 2, 2019 · 7 comments
Open

【每日一题】- 2019-09-02 - 回文 #158

azl397985856 opened this issue Sep 2, 2019 · 7 comments

Comments

@azl397985856
Copy link
Owner

@azl397985856 azl397985856 commented Sep 2, 2019

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

有这样一个乘法算式:

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

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

@azl397985856 azl397985856 changed the title 每日一题】- 2019-09-02 - 回文 【每日一题】- 2019-09-02 - 回文 Sep 2, 2019
@azl397985856

This comment has been minimized.

Copy link
Owner Author

@azl397985856 azl397985856 commented Sep 2, 2019

我这里给出一种“大人的”解法,思路就是从所有可能满足条件的数中暴力搜索即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
@azl397985856 azl397985856 self-assigned this Sep 3, 2019
@azl397985856 azl397985856 moved this from 待认领 to 正在进行 in 《每日一题》综合认领区 Sep 3, 2019
@ruoguang

This comment has been minimized.

Copy link

@ruoguang ruoguang commented Oct 8, 2019

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);
                    }
                }
            }

        }
    }
}
@azl397985856 azl397985856 removed their assignment Oct 8, 2019
@azl397985856 azl397985856 moved this from 正在进行 to 待认领 in 《每日一题》综合认领区 Oct 8, 2019
@can350925

This comment has been minimized.

Copy link

@can350925 can350925 commented Oct 9, 2019

@azl397985856

This comment has been minimized.

Copy link
Owner Author

@azl397985856 azl397985856 commented Oct 9, 2019

@bryceustc

This comment has been minimized.

Copy link

@bryceustc bryceustc commented Oct 29, 2019

因为是五个不同的数字,其实循环次数可以再减少一些,就从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;
}
@stale

This comment has been minimized.

Copy link

@stale stale bot commented Dec 28, 2019

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.

@stale stale bot added stale and removed stale labels Dec 28, 2019
@mayali123

This comment has been minimized.

Copy link

@mayali123 mayali123 commented Jan 3, 2020

#include<stdio.h>
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
5 participants
You can’t perform that action at this time.