Project Euler NO52

125874和它的二倍,251748, 包含着同樣的數字,只是順序不同。

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




import java.util.Arrays;


public class Problem52
{
	public static void main(String[] args)
	{
		long start = System.currentTimeMillis();
		System.out.print("answer:  ");
		
		howmany();
		
		long end = System.currentTimeMillis();
		System.out.print("time:  ");
		System.out.println(end - start);
	}
	
	static void howmany()
	{
		l1 : for (int i = 2; ; i++)
		{
			String str1 = (i) + "";
			String str6 = (i * 6) + "";
			
			if (str1.length() != str6.length())
			{
				continue;
			}
			String str2 = (i * 2) + "";
			String str3 = (i * 3) + "";
			String str4 = (i * 4) + "";
			String str5 = (i * 5) + "";
			
			char ch[][] = new char[6][str1.length()];
			ch[0] = str1.toCharArray();
			ch[1] = str2.toCharArray();
			ch[2] = str3.toCharArray();
			ch[3] = str4.toCharArray();
			ch[4] = str5.toCharArray();
			ch[5] = str6.toCharArray();
			String s[] = new String[6];
			for (int j = 0; j < 6; j++)
			{
				Arrays.sort(ch[j]);
				s[j] = "";
				for (int m = 0; m < str1.length(); m++)
				{
					s[j] += ch[j][m]+"";
				}
			}
			
			for (int j = 1; j < 6; j++)
			{
				if (!s[0].equals(s[j]))
				{
					continue l1;
				}
			}
			System.out.println(i);
			break;
		}
	}
}


answer:  142857
time:  990

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章