使用 STL 基于因子数量排序
使用 STL 对向量进行排序非常简单。我们可以使用著名的 sort() 函数来执行此任务。真正的挑战是计算每个数字的因子数量。
因子是指完全整除另一个数字的数字,即余数为零。
遍历所有数字以计算因子可能是一种方法,但我们将在本文中尝试优化并找到有效的解决方案。
问题陈述
根据每个数字的因子数量对给定数组进行排序(升序)。因此,因子数量最少的数字应该位于开头,因子数量最多的数字应该位于末尾。因子数量相同的数字应该与原始数组中的顺序相同。可以使用 STL 对数组进行排序。
示例
Input − Array a = [15,2,20,3,10,4] Output − 3 2 4 10 15 20 pre class="just-code notranslate language-cpp" data-lang="cpp"> The number of factors of 15 − 4. The number of factors of 2 − 2. The number of factors of 20 − 6. The number of factors of 3 − 2. The number of factors of 10 − 4. The number of factors of 4 − 3.
因此,根据因子对数字进行升序排序后,我们得到输出:3 2 4 10 15 20。
Input − Array a = [5,9,12,19,21] Output − 19 5 9 21 12
解释
The number of factors of 5 − 3. The number of factors of 9 − 3. The number of factors of 12 − 4. The number of factors of 19 − 2. The number of factors of 21 − 4.
因此,根据因子对数字进行升序排序后,我们得到输出:19 5 9 21 12。
方法
找到每个数字的因子数量。
创建一个保存数字及其因子计数记录的配对向量。
对向量进行排序并返回结果。
查找数字的因子数量
暴力法
一个简单的方法是从 1 到 n 遍历所有数字,并找出它们是否整除 n。这样,我们就可以计算每个数字的因子数量。
示例
下面是一个使用暴力法计算所有除数的 C++ 程序
#include <bits/stdc++.h>
using namespace std;
// function to count the divisors
int countDivisors(int n){
int count = 0;
for (int i = 1; i <= n; i++){
if (n % i == 0)
count++;
}
return count;
}
int main(){
int n = 55;
//Function call
int ans = countDivisors(n);
cout <<"The number of divisors of 55 is: "<<ans<<endl;
return 0;
}
输出
The number of divisors of 55 is: 4
有效方法
一个数字的除数成对存在。
例如,12 的除数是 1、2、3、4、6、12。
但是,我们可以这样可视化它们:(1,12)、(2,6)、(3,4)。
因此,如果我们找到一个除数,我们也可以找到另一个除数,我们不需要遍历到 n。
因此,有效的方法是只遍历到数字的平方根,然后成对计算除数。
示例
下面是一个计算数字的除数的 C++ 程序
#include <bits/stdc++.h>
using namespace std;
// Function to count the divisors of a number
int countDivisors(int n){
int count = 0;
for (int i=1; i<=sqrt(n); i++){
if (n%i == 0){
// If divisors are equal, count only one
if (n/i == i)
count++;
else // Otherwise count both
count += 2;
}
}
return count;
}
int main(){
int n = 55;
int ans = countDivisors(n);
cout <<"The number of divisors of 55 is: "<<ans<<endl;
return 0;
}
输出
The number of divisors of 55 is: 4
现在,我们可以遵循上面讨论的方法的第 2 步和第 3 步。
基于因子数量打印排序向量的示例 C++ 程序
#include <bits/stdc++.h>
using namespace std;
// Function to count the divisors of a number
int countDivisors(int n){
int count = 0;
for (int i=1; i<=sqrt(n); i++){
if (n%i == 0){
// If divisors are equal, count only one
if (n/i == i)
count++;
else // Otherwise count both
count += 2;
}
}
return count;
}
int main(){
int n = 5;
vector<int>vec;
//Inserting input
vec.push_back(5);
vec.push_back(14);
vec.push_back(18);
vec.push_back(9);
vec.push_back(10);
//Vector of pairs to store the number and its factor count
vector<pair<int,int>>count_data(n);
for(int i=0;i<n;i++){
//Storing the data in the vector
count_data[i] = {countDivisors(vec[i]), vec[i]};
}
//Sort the vector according to the number of factors
sort(count_data.begin(),count_data.end());
//Printing the result
cout<<"The sorted vector based on the number of factors is: \n";
for(int i=0;i<n;i++){
cout<<count_data[i].second<<" ";
}
return 0;
}
输出
The sorted vector based on the number of factors is: 5 9 10 14 18
结论
在本文中,我们根据其因子的数量对整数向量进行了排序。
我们讨论了一些示例,然后讨论了方法。
此问题的核心是找到数字的除数个数。解决此问题可能有两种方法:暴力法和有效方法。我们看到了这两种方法,然后利用有效的方法编写了最终程序。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP