Cho một số n, chúng ta cần tìm tổng số các chữ số của nó như vậy: & nbsp;n, we need to find the sum of its digits such that:
If n < 10 digSum[n] = n Else digSum[n] = Sum[digSum[n]]
Ví dụ: & nbsp;
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 4
Cách tiếp cận vũ phu là tổng hợp tất cả các chữ số cho đến khi tổng //en.wikipedia.org/wiki/digital_root
Dưới đây là việc thực hiện ý tưởng trên: & NBSP;
C++
#include
using
namespace
std;
int
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 40__
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 42
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 43
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
13
14
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 991
90
#include
3Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
90
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 996
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
int
94
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 43
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
int
903
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
905
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
90
#include
3If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
Java
#include
5
911
#include
8
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 482
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
using
1 int
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 40
int
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 42
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 43
12
13
std;
4namespace
1namespace
912
90
namespace
1namespace
212
90
933
namespace
8Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
#include
7 using
1 Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 413
948
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 43
12
int
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 419
954
namespace
212
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 423
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
Python3
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 429
962
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
13
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 446
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 433
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 433
namespace
1Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 443
12
90
namespace
1Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
13
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 446
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 464
namespace
8 Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 433
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 433
namespace
1__
12
90
namespace
8Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
986
987
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 991
90
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 446
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 464
namespace
8namespace
9Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 452
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 433
954
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 477
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 478
C#
using
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 480
#include
8 #include
02
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 43
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
using
1 int
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 40
int
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 42
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 43
12
13
std;
4namespace
1namespace
912
90
namespace
1namespace
212
90
933
namespace
8Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
#include
7 using
1 Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 413
948
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 43
12
int
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 419
954
namespace
212
132
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10,
digSum[x] == 10
Hence ans will be 1+0 = 1
Input : 5674
Output : 4
29 9
62
136
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
13
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 446
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 433
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 433
namespace
1Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 443
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 43
12
90
namespace
112
90
namespace
1namespace
212
90
933
namespace
8If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
#include
7 using
1 Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 413
948
12
int
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 419
954
namespace
2Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 429
962
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10,
digSum[x] == 10
Hence ans will be 1+0 = 1
Input : 5674
Output : 4
41
3 Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10,
digSum[x] == 10
Hence ans will be 1+0 = 1
Input : 5674
Output : 4
46Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10,
digSum[x] == 10
Hence ans will be 1+0 = 1
Input : 5674
Output : 4
33Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10,
digSum[x] == 10
Hence ans will be 1+0 = 1
Input : 5674
Output : 4
33 namespace
1Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10,
digSum[x] == 10
Hence ans will be 1+0 = 1
Input : 5674
Output : 4
43
195
12
90
namespace
1Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 43
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
13
14
12
90
namespace
1namespace
212
90
933
namespace
8If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
903
#include
82
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 928
Output:
9
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10,
digSum[x] == 10
Hence ans will be 1+0 = 1
Input : 5674
Output : 4
4#include
7 using
1 Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10,
digSum[x] == 10
Hence ans will be 1+0 = 1
Input : 5674
Output : 4
13 9
48 O[1]
1
2int
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10,
digSum[x] == 10
Hence ans will be 1+0 = 1
Input : 5674
Output : 4
199
54namespace
2 O[1]
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10,
digSum[x] == 10
Hence ans will be 1+0 = 1
Input : 5674
Output : 4
29 9
62
//www.geeksforgeeks.org/digital-rootrepeated-digital-sum-given-integer/
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 44
13
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 446
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 433
Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 433
namespace
1Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum[x] == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 443Ayush Khanduri. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.