?
求解思路:
现在分析一个问题,假设将十位数为a,个位数为b的一个整数表示为ab,则推导得
ab*ab = (a*10+b)*(a*10+b) = 100*a*a+10*2*a*b+b*b
根据上式可得:root(ab*ab) = a*a+2*a*b+b*b = (a+b)*(a+b);[公式一]
同理也可证得:root(ab*ab*ab) = (a+b)*(a+b)*(a+b);[公式二]
可以看出,N个相同整数的乘积总值的树根 = 每一项元素的树根的乘积
再设另外一个整数cd,且cd!=ab
ab*cd = (a*10+b)*(c*10+d) = 100*a*c+10*(a*d+b*c)+b*d
根据上式可得:root(ab*cd) = a*c+a*d+b*c+b*d = (a+b)*(c+d);[公式三]
可见,对于两个不相同整数也成立。
最后将上面证得的结果一般化:
N个整数的乘积总值的数根 = 每个项元素的数根的乘积
提示:本题只需根据[公式三] 即可AC.
注:思路确实是公式二没错,但是中间过程采用公式二会溢出,所以必须通过公式三。(思想一样)
【AC代码】:
?
#include
#include
#include
#include
#include
#include
#include
using namespace std; /* run this program using the console pauser or add your own getch, system(pause) or input loop */ int getRoot(int n) { while (n >= 10) { int sum = 0; while (n) { sum += n%10; n /= 10; } n = sum; } return n; } int main(int argc, char** argv) { //freopen(in.txt, r, stdin); //freopen(out.txt, w, stdout); int n = 0; while (cin >> n && n) { int root = getRoot(n); int i = 0, mul = 1; for (i = 0; i < n; i++) mul = getRoot(root*mul); cout << getRoot(mul) << endl; } return 0; }
?