本文介绍了在C中返回其正因子计数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行一种方法,该方法的函数名为factor_count,该函数接受整数作为其参数并返回其正因子的计数.

I am trying to do a method that its function named is factor_count that accepts an integer as its parameter and returns a count of its positive factors.

例如,六个因子32是1、2、4、8、16和32,因此我的方法调用应返回6.

For example, the six factors of 32 are 1, 2, 4, 8, 16, and 32, so the call of my method should return 6.

int factor_count(int number) {
  int i, count;
  for (i=1;i<=number;i++){
    if(number%1==0){
      count = number%i;
    }
  }
  return count;
}

推荐答案

%是模运算符.除法后剩下的.如果除法后的余数为零,则应递增计数.除以1的余数始终为零.

% is the modulo operator. It's remainder after a division. If the remainder after division is zero you should increment count. The remainder from dividing by 1 is always zero.

int factor_count(int number)
{
    int i, count = 0;

    for (i=1; i<=number; i++)
        /* increment count when the remainder is zero */
        if (number%i==0)
            count++;
    return count;
}

这篇关于在C中返回其正因子计数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:02