用C++编程来查找给定范围中拥有奇数个除数的数字数量
本教程将探讨编写一个程序来查找给定范围内拥有奇数个除数的数字数量。
为此,我们将提供该范围的上限和下限。我们的任务是计算和统计拥有奇数个除数的值的数量。
示例
#include <bits/stdc++.h>
using namespace std;
//counting the number of values
//with odd number of divisors
int OddDivCount(int a, int b){
int res = 0;
for (int i = a; i <= b; ++i) {
int divCount = 0;
for (int j = 1; j <= i; ++j) {
if (i % j == 0) {
++divCount;
}
}
if (divCount % 2) {
++res;
}
}
return res;
}
int main(){
int a = 1, b = 10;
cout << OddDivCount(a, b) << endl;
return 0;
}输出
3
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP