请写出一个方程,求反复各位数之和
直到其和为一位为止,返回这个数字
ie. 5472576 => 5 +4 +7+2+5+7+6 =36 => 3+6 =9 返回9
ie. 5472576 => 5 +4 +7+2+5+7+6 =36 => 3+6 =9 返回9
1
///
<summary>
2 /// Fun(integer,true)
3 /// </summary>
4 /// <param name="integer"></param>
5 /// <param name="b"></param>
6 /// <returns></returns>
7 static int Fun( int integer, bool b)
8 {
9 if (b)
10 {
11 while ((integer = Fun(integer, false )) > 9 ) ;
12 return integer;
13 }
14
15 int len = integer.ToString().Length;
16 if (len <= 1 ) return integer;
17
18 int n = 1 ;
19 for ( int i = 2 ; i <= len; i ++ ) n *= 10 ;
20 int m = integer / n;
21 return m + Fun(integer - n * m, false );
22 }
2 /// Fun(integer,true)
3 /// </summary>
4 /// <param name="integer"></param>
5 /// <param name="b"></param>
6 /// <returns></returns>
7 static int Fun( int integer, bool b)
8 {
9 if (b)
10 {
11 while ((integer = Fun(integer, false )) > 9 ) ;
12 return integer;
13 }
14
15 int len = integer.ToString().Length;
16 if (len <= 1 ) return integer;
17
18 int n = 1 ;
19 for ( int i = 2 ; i <= len; i ++ ) n *= 10 ;
20 int m = integer / n;
21 return m + Fun(integer - n * m, false );
22 }