欧拉工程第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 ...
随机推荐
- MVC4.0 利用HandleErrorAttribute和log4net实现记录异常日志功能
1.MVC4.0中HandleErrorAttribte已经帮我们处理了异常问题,当我们新建一个非空的MVC项目时候,在FilterConfig中会发现这样的代码 public class Filte ...
- SQL Server 2008 没有可用于 charge_sys_Log.LDF 的编辑器
因为上网问题重新装了系统.今天在整 SQL Server 的时候出现了这样一个问题. 因为之前装 SQL Server 的时候没有遇到过这种情况,感觉很新奇.所以详细的记录一下,希望对别人能有一定 ...
- LeetCode-Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- “我爱淘”冲刺阶段Scrum站立会议3
完成任务: 将搜索框的界面已经实现以及部署到整个框架中. 计划任务: 实现搜索功能,通过数据库的链接,实现用户可以查到自己需要的书籍的信息. 遇到问题: 1.数据库的操作,怎么实现查询功能: 2.Ac ...
- linux 负载 待读
相关文章: 理解 Linux 的处理器负载均值(翻译)
- SPFA 原理剖析代码实现分析比较
算法简介 SPFA(Shortest Path Faster Algorithm)是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 算法流程 算法大致流程是用一个队列来进行维护. ...
- 02.Hibernate映射基础
前言:Hibernate的核心功能是根据数据库到实体类的映射,自动从数据库绑定数据到实体类.使我们操作实体类(Java对象)就能对数据库进行增.删.查.改,而不用调用JDBC API使数据操作变得简单 ...
- 【BZOJ】【3156】防御准备
DP/斜率优化 斜率优化的裸题…… sigh……又把$10^6$当成10W了……RE了N发 这题还是很水的 当然逆序也能做……不过还是整个反过来比较顺手 反转后的a[0]=反转前的a[n],以此类推直 ...
- JS语句循环(100以备奇偶数、100以内与7先关的数、100以内整数的和、10以内阶乘、乘法口诀、篮球弹起高度、64格子放东西)
3.循环 循环是操作某一个功能(执行某段代码). ①循环四要素: a 循环初始值 b 循环的条件 c 循环状态 d 循环体 ②for循环 a 穷举:把所有的可能性的都一一列出来. b 迭代:每次循环都 ...
- linux - 使用curl实现新浪天气API应用
新浪天气API的使用方法: API地址:http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT82 ...