|
原帖由 newkid 于 10-11-10 22:41 发表 ![]()
本周的题相对简单:
#18 Three Digits
You have a number where any numeral appears at most twice. The sum of all neighboring three digits in this number is a square number.
What is the maximum possible value for this number?
Example: 74504 is such a number because no numeral appears more than twice and 7+4+5, 4+5+0 and 5+0+4 are square numbers.
有一个数,每一位最多出现两次,所有相邻的三位数值和都是一个平方数。这个数可能的最大值是多少?
比如:74504就是这样一个数,它的所有位都不超过两次,并且7+4+5, 4+5+0 和 5+0+4都是平方数。
WITH t(str) AS (
SELECT TO_CHAR(ROWNUM) FROM DUAL CONNECT BY ROWNUM<=9
UNION ALL
SELECT t.str||n
FROM t,(SELECT ROWNUM-1 n FROM DUAL CONNECT BY ROWNUM<=10)
WHERE INSTR(str,n,1,2)=0 AND (LENGTH(str)<=2 OR SUBSTR(str,-1,1)+SUBSTR(str,-2,1)+n IN (4,9,16,25))
)
SELECT MAX(str) KEEP(DENSE_RANK FIRST ORDER BY LENGTH(str) DESC) FROM T;
9871801306376
9+8+7=24,明显不是一个平方数
另外,(4,9,16,25)也应改为(1,4,9,16,25)
1004这样的数字显然是满足 相邻三位和为平方数的 要求的一个数 |
|