這題我是用暴力解的.但解了之後,看別人其實有特解,
就是直接輸出9-apple
原因在於:
假設n 是a + 10*b + 100*c + 1000*d
Sum all the digits of n的結果是s.
s=a+b+c+d
2. Subtract the sum from n, and it is your new n.
所以 new n = n - s
= a + 10*b + 100*c + 1000*d - (a + b + c + d)
= 9*b + 99*c + 999*d
所以一定是9的倍數。
其餘不是千位數的n類推,也是9的倍數
3. If the new n is in the list below return the associated fruit, otherwise return back to task 1.
...
9-apple
...
18-apple
...
27-apple
...
36-apple
...
45-apple
...
54-apple
...
63-apple
...
72-apple
...
81-apple
...
90-apple
91-kiwi
92-banana
93-kiwi
94-banana
95-melon
96-banana
97-melon
98-pineapple
99-apple
100-pineapple
9的倍數,相關的水果一定是apple.不會是別的。
沒有留言:
張貼留言