Project Euler NO36

十進制數字585 = 10010010012 (二進制),可以看出在十進制和二進制下都是迴文(從左向右讀和從右向左讀都一樣)。

求100萬以下所有在十進制和二進制下都是迴文的數字之和。

(注意在兩種進制下的數字都不包括最前面的0)




public class Problem36
{
	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()
	{
		int sum = 0;
		l1 : for (int i = 1; i < 1000000; i++)
		{
			char array[] = (i + "").toCharArray();
			for (int j = 0; j < array.length; j++)
			{
				if (array[j] != array[array.length - 1 - j])
				{
					continue l1;
				}
			}
			
			String i2 = Integer.toBinaryString(i);
			char array2[] = i2.toCharArray();
			for (int j = 0; j < array2.length; j++)
			{
				if (array2[j] != array2[array2.length - 1 - j])
				{
					continue l1;
				}
			}
			
			sum += i;
		}
		
		System.out.println(sum);
	}
}


answer:  872187
time:  208


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