{"trustable":true,"prependHtml":"\u003cstyle type\u003d\"text/css\"\u003e\n #problem-body \u003e pre {\n display: block;\n padding: 9.5px;\n margin: 0 0 10px;\n font-size: 13px;\n line-height: 1.42857143;\n word-break: break-all;\n word-wrap: break-word;\n color: #333;\n background: rgba(255, 255, 255, 0.5);\n border: 1px solid #ccc;\n border-radius: 6px;\n }\n\u003c/style\u003e\n","sections":[{"title":"","value":{"format":"HTML","content":"\u003cdiv id\u003d\"problem-body\"\u003e\n\t\u003cp\u003eThe process of “\u003cstrong\u003ebreaking\u003c/strong\u003e” an integer is defined as summing the squares of its digits. For example, the result of breaking the integer \u003cstrong\u003e125\u003c/strong\u003e is (\u003cstrong\u003e1\u003csup\u003e2\u003c/sup\u003e\u003c/strong\u003e + \u003cstrong\u003e2\u003csup\u003e2\u003c/sup\u003e\u003c/strong\u003e + \u003cstrong\u003e5\u003csup\u003e2\u003c/sup\u003e\u003c/strong\u003e) \u003d \u003cstrong\u003e30\u003c/strong\u003e. An integer \u003cstrong\u003eN\u003c/strong\u003e is \u003cstrong\u003ehappy\u003c/strong\u003e if after “\u003cstrong\u003ebreaking\u003c/strong\u003e” it repeatedly the result reaches 1. If the result never reaches 1 no matter how many times the “\u003cstrong\u003ebreaking\u003c/strong\u003e” is repeated, then N is not a happy number.\u003c/p\u003e\r\n\r\n\u003ch3\u003eTask\u003c/h3\u003e\r\n\u003cp\u003eWrite a program that given an integer \u003cstrong\u003eT\u003c/strong\u003e (number of test cases) and \u003cstrong\u003eT\u003c/strong\u003e integers, determines for each number whether it is a happy number or not.\u003c/p\u003e\r\n\r\n\u003ch3\u003eConstraints\u003c/h3\u003e\r\n\u003cp\u003e1 ≤ T ≤ 1,080,000\u003c/p\u003e\r\n\u003cp\u003e2 ≤ N ≤ 2,147,483,647 (number for determining whether it is happy or not)\u003c/p\u003e\r\n\r\n\u003ch3\u003eInput\u003c/h3\u003e\r\n\u003cp\u003eThe first line contains an integer \u003cstrong\u003eT\u003c/strong\u003e.\u003c/p\u003e\r\n\u003cp\u003eNext T lines contain an integer \u003cstrong\u003eN\u003c/strong\u003e for detemining whether it is happy or not.\u003c/p\u003e\r\n\r\n\u003ch3\u003eOutput\u003c/h3\u003e\r\n\u003cp\u003eT lines containing a single integer \u003cstrong\u003eN\u003c/strong\u003e which is the number of times the process had to be done to determine that N is happy, or \u003cstrong\u003e-1\u003c/strong\u003e if \u003cstrong\u003eN\u003c/strong\u003e is not happy.\u003c/p\u003e\r\n\r\n\u003ch3\u003eExample\u003c/h3\u003e\r\n\u003cdiv\u003e\u003ctable class\u003d\"vjudge_sample\"\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e2\n19\n204\n\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e4\n-1\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/div\u003e\r\n\r\n\u003ch3\u003eExplanation\u003c/h3\u003e\r\n\u003cp\u003e\u003cstrong\u003eFirst test case\u003c/strong\u003e:\u003c/p\u003e\r\n\u003cul\u003e\r\n\u003cli\u003e19 : 1\u003csup\u003e2\u003c/sup\u003e + 9\u003csup\u003e2\u003c/sup\u003e \u003d 82\u003c/li\u003e\r\n\u003cli\u003e82 : 82 + 2\u003csup\u003e2\u003c/sup\u003e \u003d 68\u003c/li\u003e\r\n\u003cli\u003e68 : 6\u003csup\u003e2\u003c/sup\u003e + 8\u003csup\u003e2\u003c/sup\u003e \u003d 100\u003c/li\u003e\r\n\u003cli\u003e100 : 1\u003csup\u003e2 \u003c/sup\u003e+ 0\u003csup\u003e2\u003c/sup\u003e + 0\u003csup\u003e2\u003c/sup\u003e \u003d 1\u003c/li\u003e\r\n\u003c/ul\u003e\r\n\u003cp\u003eThe solution for 19 is 4 because we discovered that the integer 19 is happy after we repeated the process 4 times.\u003c/p\u003e\r\n\r\n\u003cp\u003e\u003cstrong\u003eSecond test case\u003c/strong\u003e:\u003c/p\u003e\r\n\u003cp\u003e204 → \u003cb\u003e20\u003c/b\u003e → 4 → 16 → 37 → 58 → 89 → 145 → 42 → \u003cb\u003e20\u003c/b\u003e → 4 → 16 → 37 → 58 → 89 → 145 ...\u003c/p\u003e\r\n\u003cp\u003e\u003cstrong\u003e204\u003c/strong\u003e is not a happy number because after breaking it several times the results start repeating so we can deduce that if we continue breaking it, the result will never reach 1.\u003c/p\u003e\r\n\n\u003c/div\u003e"}}]}