从今往后我会不定期的写一些关于Codewars上面关于C语言的一些题目,个人认为这些题目用来练手真是再好不过了,但是没有leetcode上的题难,一步一步来嘛,上面的有些题目也很精彩很考验基本功。
我首先贴出问题,然后贴出自己的代码,再在我代码下面贴出我认为最厉害的解法(啪啪打脸)。当然,高手轻喷,能学到东西就好!
那么来看第一道吧!
问题描述:
Some numbers have funny properties. For example:
89 --> 8¹ + 9² = 89 * 1
695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2
46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
Given a positive integer n written as abcd… (a, b, c, d… being digits) and a positive integer p we want to find a positive integer k, if it exists, such as the sum of the digits of n taken to the successive powers of p is equal to k * n. In other words:
Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + …) = n * k
If it is the case we will return k, if not return -1.
No