输入格式:
输入在一行中给出2个正整数m和n(1<m≤n≤1000),中间以空格分隔。
输出格式:
逐行输出给定范围内每个完数的因子,每个完数占一行,格式为“完数 its factors are 1,2,3”,其中完数和因子均按递增顺序给出。若区间内没有完数,则输出“Not Found!”。
输入样例1:
3 30
输出样例1:
1. 6 its factors are 1,2,3 2. 28 its factors are 1,2,4,7,14
输入样例2:
600 900
输出样例2:
Not Found!
#include <iostream> #include <algorithm> #include <cstring> #include <queue> #include <stack> #include <vector> #include <set> #include <map> #include <unordered_map> #include <unordered_set> using namespace std; #define endl '\n' const int N = 10; int m, n, flag; int main() { ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin >> m >> n; for (int i = m; i <= n; i ++) { int sum = 0; vector<int> arr; for (int j = 1; j < i / 2 + 1; j ++) { if (i % j == 0) { arr.push_back(j); sum += j; } } if (sum == i) { cout << i << " its factors are "; for (int j = 0; j < arr.size(); j ++) { if (j) cout << ','; cout <<arr[j], flag = 1; } cout << endl; } } if (!flag) cout << "Not Found!" << endl; return 0; }