1
int N = 2345;
        var digitArray = N.ToString().ToCharArray();
        int multiplicationSum = 1;
        foreach (char digit in digitArray)
        {
            multiplicationSum = int.Parse(digit.ToString()) * multiplicationSum;
        }
        var sum = 0;
        var sumArray = multiplicationSum.ToString().ToCharArray();
        foreach (char digit in sumArray)
        {
            sum = int.Parse(digit.ToString()) + sum;
        }

谢谢

4

1 回答 1

7

假设您不需要处理负数,似乎在数学上这样做会更有意义

int N = 2345;
int multiplicationSum = 1;
while(N!=0)
{
  multiplicationSum = multiplicationSum * (N%10);
  N = N/10;
}
var sum = 0;
while(multiplicationSum!=0)
{
  sum = sum + (multiplicationSum%10);
  multiplicationSum = multiplicationSum/10;
}

参考:

% 操作员

% 运算符计算第一个操作数除以第二个操作数后的余数

/ 运算符

当您将两个整数相除时,结果始终是整数。例如,7 / 3 的结果是 2

于 2013-06-13T06:13:13.180 回答