Skip to content

Commit 3ec0b58

Browse files
committed
add digits
1 parent 267cee4 commit 3ec0b58

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

Math/Math.Lib/AddDigitsSln.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
// // Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
6+
7+
// // For example:
8+
9+
// // Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
10+
11+
// // Follow up:
12+
// // Could you do it without any loop/recursion in O(1) runtime?
13+
namespace Math.Lib
14+
{
15+
public class AddDigitsSln
16+
{
17+
public int AddDigits(int num) {
18+
return num - 9*Convert.ToInt32((num-1)/9);
19+
}
20+
}
21+
22+
}

Math/Math.Lib/PalindromeNumberSln.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* ==============================================================================
2+
* 功能描述:PalindromeNumberSln
3+
* 创 建 者:gz
4+
* 创建日期:2017/5/24 13:01:57
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
11+
namespace Math.Lib
12+
{
13+
/// <summary>
14+
/// PalindromeNumberSln
15+
/// </summary>
16+
public class PalindromeNumberSln
17+
{
18+
public bool IsPalindrome(int x)
19+
{
20+
int palindromex = 0, tmp = x;
21+
int sumbit = 0;
22+
//calcuate bit count
23+
while (tmp > 0)
24+
{
25+
sumbit++;
26+
tmp /= 10;
27+
}
28+
tmp = x;
29+
while (tmp > 0)
30+
{
31+
palindromex += tmp % 10 * (int)System.Math.Pow(10, --sumbit);
32+
tmp /= 10;
33+
}
34+
return palindromex == x;
35+
}
36+
37+
}
38+
}

0 commit comments

Comments
 (0)