C#中的数字总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/478968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sum of digits in C#
提问by Xn0vv3r
What's the fastest and easiest to read implementation of calculating the sum of digits?
计算数字总和的最快和最容易阅读的实现是什么?
I.e. Given the number: 17463 = 1 + 7 + 4 + 6 + 3 = 21
即给定数字:17463 = 1 + 7 + 4 + 6 + 3 = 21
采纳答案by Greg Hewgill
You could do it arithmetically, without using a string:
您可以在不使用字符串的情况下进行算术运算:
sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
回答by Jon Skeet
public static int SumDigits(int value)
{
int sum = 0;
while (value != 0)
{
int rem;
value = Math.DivRem(value, 10, out rem);
sum += rem;
}
return sum;
}
回答by Chaowlert Chaisrichalermpol
I use
我用
int result = 17463.ToString().Sum(c => c - '0');
It uses only 1 line of code.
它仅使用 1 行代码。
回答by Tjipke
I like the chaowman's response, but would do one change
我喜欢 chaowman 的回应,但会做一个改变
int result = 17463.ToString().Sum(c => Convert.ToInt32(c));
I'm not even sure the c - '0', syntax would work? (substracting two characters should give a character as a result I think?)
我什至不确定 c - '0',语法是否有效?(我认为减去两个字符应该得到一个字符?)
I think it's the most readable version (using of the word sum in combination with the lambda expression showing that you'll do it for every char). But indeed, I don't think it will be the fastest.
我认为这是最易读的版本(将单词 sum 与 lambda 表达式结合使用,表明您将为每个字符执行此操作)。但事实上,我认为它不会是最快的。
回答by Ants
For integer numbers, Greg Hewgill has most of the answer, but forgets to account for the n < 0. The sum of the digits of -1234 should still be 10, not -10.
对于整数,Greg Hewgill 有大部分答案,但忘记考虑 n < 0。-1234 的数字总和应该仍然是 10,而不是 -10。
n = Math.Abs(n);
sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
It the number is a floating point number, a different approach should be taken, and chaowman's solution will completely fail when it hits the decimal point.
如果数字是浮点数,则应采取不同的方法,而chaowman的解决方案在达到小数点时将完全失败。
回答by Muhammad Hasan Khan
int num = 12346;
int sum = 0;
for (int n = num; n > 0; sum += n % 10, n /= 10) ;
回答by sindre j
I would suggest that the easiest to read implementation would be something like:
我建议最容易阅读的实现是这样的:
public int sum(int number)
{
int ret = 0;
foreach (char c in Math.Abs(number).ToString())
ret += c - '0';
return ret;
}
This works, and is quite easy to read. BTW: Convert.ToInt32('3') gives 51, not 3. Convert.ToInt32('3' - '0') gives 3.
这是有效的,并且很容易阅读。顺便说一句:Convert.ToInt32('3') 给出 51,而不是 3。Convert.ToInt32('3' - '0') 给出 3。
I would assume that the fastest implementation is Greg Hewgill's arithmetric solution.
我认为最快的实现是 Greg Hewgill 的算术解决方案。
回答by Nolonar
I thought I'd just post this for completion's sake:
我以为我只是为了完成而发布这个:
If you need a recursive sum of digits, e.g: 17463-> 1 + 7 + 4 + 6 + 3 = 21-> 2 + 1 = 3
then the best solution would be
如果您需要递归的数字总和,例如:17463-> 1 + 7 + 4 + 6 + 3 = 21-> 2 + 1 = 3
那么最好的解决方案是
int result = input % 9;
return (result == 0 && input > 0) ? 9 : result;
回答by Pankaj Prakash
The simplest and easiest way would be using loops to find sum of digits.
最简单和最简单的方法是使用循环来查找数字总和。
int sum = 0;
int n = 1234;
while(n > 0)
{
sum += n%10;
n /= 10;
}
回答by Saravanan Daya
int j, k = 1234;
for(j=0;j+=k%10,k/=10;);