C++ 程序中不是 n 的因数的 n 平方的因数
在本教程中,我们将编写一个程序来查找 n 平方和非 n 的约数的个数。
这是一个非常简单的问题。我们来看看解决这个问题的步骤。
初始化数字 n。
初始化约数的计数器。
从 2 迭代到 n^2n2。
如果 n^2n2 被当前数字整除且 nn 未被当前数字整除,则增加计数。
打印计数。
示例
让我们看看代码。
#include <bits/stdc++.h>
using namespace std;
int getNumberOfDivisors(int n) {
int n_square = n * n;
int divisors_count = 0;
for (int i = 2; i <= n_square; i++) {
if (n_square % i == 0 && n % i != 0) {
divisors_count++;
}
}
return divisors_count;
}
int main() {
int n = 6;
cout << getNumberOfDivisors(n) << endl;
return 0;
}输出
如果你执行上面程序,那么你将得到以下结果。
5
结论
如果你在教程中有任何疑问,请在评论部分提及。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP