欧拉工程第52题:Permuted multiples

时间:2021-04-12 13:47:18

题目链接

题目:

125874和它的二倍,251748, 包含着同样的数字,只是顺序不同。

找出最小的正整数x,使得 2x, 3x, 4x, 5x, 和6x都包含同样的数字。

这个题目相对比较简单

暴力遍历

判断x,2x,3x,4x,5x,6x是否包含的数字相同

如何判断两个数包含的数字相同?

1.

两个数字转换成字符串后:d1,d2

定义两个集合ts1,ts2,

将d1,d2的各位数都添加到集合ts1中

将d1的各位数添加到集合ts2中

最后这个两个集合相等,则,这个两个数包含相同的数字

2.

public  boolean permutation(int a, int b)
{
int[] nums = new int[10];
int temp1 = a;
int temp2 = b;
while(temp1>0 && temp2>0)
{
nums[temp1%10]++;
nums[temp2%10]--;
temp1/=10;
temp2/=10;
}
for(int c = 0;c<10;c++)
{
if(nums[c] != 0)
return false;
}
return true;
}

看程序吧,描述不好了。。。

java程序:

package projecteuler51to60;

import java.util.Set;
import java.util.TreeSet; class level52{
void solve0(){
int Max_Value=1000000; for(int i=2;i<Max_Value;++i){
if(sameDigit(i,2*i) &&sameDigit(i,3*i) &&
sameDigit(i,4*i) &&sameDigit(i,5*i) &&
sameDigit(i,6*i)){
System.out.println(i);
return;
}
} }
void solve1(){
int orig=1;
int ans=0;
while(ans==0){
int count=1;
for(int a=2;a<=6;a++){
int b=orig*a;
if(permutation(orig,b))
count++;
}
if(count==6)
ans=orig;
orig++;
}
System.out.println(ans);
}
public boolean permutation(int a, int b)
{
int[] nums = new int[10];
int temp1 = a;
int temp2 = b;
while(temp1>0 && temp2>0)
{
nums[temp1%10]++;
nums[temp2%10]--;
temp1/=10;
temp2/=10;
}
for(int c = 0;c<10;c++)
{
if(nums[c] != 0)
return false;
}
return true;
} boolean sameDigit(int a,int b){
String stra=String.valueOf(a);
String strb=String.valueOf(b);
Set<String> set=new TreeSet<String>();
Set<String> setEqual=new TreeSet<String>();
if(stra.length()!=strb.length()) return false;
for(int i=0;i<stra.length();i++){
set.add(stra.substring(i,i+1));
set.add(strb.substring(i,i+1));
setEqual.add(strb.substring(i,i+1)); }
return set.equals(setEqual);
} }
public class Problem52 { public static void main(String[] args){
long begin= System.currentTimeMillis();
new level52().solve0();
long end = System.currentTimeMillis();
long Time = end - begin;
System.out.println("Time:"+Time/1000+"s"+Time%1000+"ms");
} }

欧拉工程第52题:Permuted multiples的更多相关文章

  1. 欧拉工程第69题:Totient maximum

    题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数*链接 这里的是p是n ...

  2. 欧拉工程第70题:Totient permutation

    题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...

  3. 欧拉工程第67题:Maximum path sum II

    By starting at the top of the triangle below and moving to adjacent numbers on the row below, the ma ...

  4. 欧拉工程第66题:Diophantine equation

    题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...

  5. 欧拉工程第65题:Convergents of e

    题目链接 现在做这个题目真是千万只*在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...

  6. 欧拉工程第56题:Powerful digit sum

    题目链接   Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...

  7. 欧拉工程第55题:Lychrel numbers

    package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; import java.util ...

  8. 欧拉工程第54题:Poker hands

    package projecteuler51to60; import java.awt.peer.SystemTrayPeer; import java.io.BufferedReader; impo ...

  9. 欧拉工程第53题:Combinatoric selections

    package projecteuler51to60; class p53{ void solve1(){ int count=0; int Max=1000000; int[][] table=ne ...

随机推荐

  1. iOS开发之Bug(持续更新)

    前言:收集在开发和学习的过程中遇到的bug. 1.循环利用cell的ID设置位置写错了.导致程序奔溃. 2.对于除数算法,可以直接算出结果的就写上结果,不要偷懒写式子让计算机自己算,更何况是除数,会有 ...

  2. MVC中的&commat;section

    在前文<MVC中 _ViewStart _Layout Index三个页面中的加载顺序> 中另外指定母版页 Layout.cshtml时...遇到了这个问题.. 报错: 以下各节已定义,但 ...

  3. mysql数据库参数innodb&lowbar;buffer&lowbar;pool&lowbar;size和max&lowbar;connections

    接到报故,查看mysql数据库以下参数 1.innodb_buffer_pool_size 2.max_connections 该参数定义了数据缓冲区buffer pool大小,类似于oracle的d ...

  4. flex4 list 自动适应高度

    <s:List width="100%"> <s:layout> <s:VerticalLayout useVirtualLayout="f ...

  5. Java 8 基础教程 - Predicate

    在Java 8中,Predicate是一个函数式接口,可以被应用于lambda表达式和方法引用.其抽象方法非常简单: /** * Evaluates this predicate on the giv ...

  6. Django实战&lpar;一&rpar;-----用户登录与注册系统3(前端页面、登录视图)

    基本框架搭建好了后,我们就要开始丰富页面内容了.最起码,得有一个用户登录的表单不是么?(注册的事情我们先放一边.) 一. 原生HTML页面 删除原来的login.html文件中的内容,写入下面的代码: ...

  7. Android UiAutomator - CTS Frame

    使用UiAutomator进行UI自动化测试后,生成的测试结果并不是很美观.为了生成一份好看的测试结果(报告),本文将使用CTS框架,当然也可以自己编写一份测试报告框架(如:生成html,excel报 ...

  8. TypeError&colon; Fetch argument None has invalid type &lt&semi;type &&num;39&semi;NoneType&&num;39&semi;&gt&semi;

    (fetch, type(fetch)))TypeError: Fetch argument None has invalid type <type 'NoneType'> 我的解决方案是 ...

  9. python---session(最终版)&lowbar;&lowbar;setitem&lowbar;&lowbar;和&lowbar;&lowbar;getitem&lowbar;&lowbar;方法

    一般来说对于其他语言session值一般获取方法为session['name'],赋值使用session['name']=val 对于python类中含有一些魔术方法__setitem__,__get ...

  10. pip安装python模块遇到一直出现retrying的问题

    最近安装python模块,遇到这样的一个问题如图所示: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status= ...