欧拉工程第52题:Permuted multiples
题目链接
题目:
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的更多相关文章
- 欧拉工程第69题:Totient maximum
题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...
- 欧拉工程第70题:Totient permutation
题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...
- 欧拉工程第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 ...
- 欧拉工程第66题:Diophantine equation
题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...
- 欧拉工程第65题:Convergents of e
题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...
- 欧拉工程第56题:Powerful digit sum
题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...
- 欧拉工程第55题:Lychrel numbers
package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; import java.util ...
- 欧拉工程第54题:Poker hands
package projecteuler51to60; import java.awt.peer.SystemTrayPeer; import java.io.BufferedReader; impo ...
- 欧拉工程第53题:Combinatoric selections
package projecteuler51to60; class p53{ void solve1(){ int count=0; int Max=1000000; int[][] table=ne ...
随机推荐
- 【转载】about slack
About Slack slack is the difference b/w the REQUIRED TIME and the ARRIVAL TIME. 1.WHAT IS SLACK WITH ...
- 28335 sci fifo send
#include "DSP2833x_Device.h"#include "DSP2833x_Examples.h"char buf[]={0x30,0x32, ...
- PB建数据窗口的时候会报内存错误
同事碰到了这个问题,百度了一下,按照下边的方法解决了 ------解决方案--------------------我遇到过,是powerbuilder的注册表出问题了,找到注册表中HKEY_USER ...
- Oracle小技巧
Oracle纵向变横向,多行变一行 取处方下的药品大类,以 处方 药品大类 a001 中药.西药,中成药的 的格式显示 /*处方与药品大类的对应 周璇球 20130829*/ SELEC ...
- linux matplotlib入门
python linux matplotlib 安装: sudo apt-get install python-numpy 必须 先安装numpy matplotlib 安装: sudo ap ...
- UITableView 应用及其总结
Plain: Grouped: Cell的结构图: UITableViewCellStyleDefault:预设使用这种,若左侧ImageView没图的话,只有一行字(textLable.text). ...
- 使用Log4j进行日志操作
使用Log4j进行日志操作 一.Log4j简介 (1)概述 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接字服 ...
- linux下的clock skew detected
今天在虚拟机上用GCC编译一个程序的时候,出现了下面的错误: make: warning: Clock skew detected. Your build may be incomplete 试了ma ...
- Python 删除列表中的重复数据
list0=['b','c', 'd','b','c','a','a'] 方法1:使用set() list1=sorted(set(list0),key=list0.index) # sorted o ...
- css3 的content 属性
content属性想必大家都熟悉了,一般结合伪类一起使用,表示显示的内容 例如:.box:before{content:"hello";width:100px;line-heigh ...