完数的定义:如果一个大于1的正整数的所有因子之和等于它的本身,则称这个数是完数,比如6,28都是完数:6=1+2+3;28=1+2+4+7+14。
本题的任务是判断两个正整数之间完数的个数。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n , num1 , num2 ;
cin >> n ;
for (int a = 0 ; a < n ;a++)
{
cin >> num1 >> num2;
int i = 0;
int k = 1;
int l = 0;
for(int b = 0; b < num2-num1+1 ; b++ )
{
i = num1 + b;
for(int c = 1; c < i; c++)
{
for(int d =1 ; d < i ; d++)
{
if( c*d == i )
{
k=k+d;
}
}
}
if ( k == i)
l = l + 1;
}
cout << l << endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main() {
int n , num1 , num2;
cin >> n ;
for (int a = 0 ; a < n ; a++) {
cin >> num1 >> num2;
int l = 0;
for(int b = num1; b <= num2; b++ ) {
int k=0;
for(int c = 1; c < b; c++) {
if( b%c==0 ) {
k=k+c;
}
}
if ( k == b)
l = l + 1;
}
cout << l << endl;
}
return 0;
}
觉得有用的话采纳一下哈