解题思路:
void cheaknum(int n)//利用vector储存所有N以内每个数的因子,检查满足条件就打印
注意事项:
现在研究出来的完数均是偶数,所以先判断是否是偶数偷懒可以有效减少运算时间
参考代码:
#include<iostream>
#include<vector>
using namespace std;
void cheaknum(int n)
{ vector<int> factor(1);
int sum = 0;
factor[0] = 1;
for (int f1 = 2; f1 <= (n / 2); f1++) {
if ((n % f1) == 0) {
factor.push_back(f1);
}
}
for (vector<int>::iterator it = factor.begin(); it != factor.end(); it++)
{
sum = sum + (*it);
}
if(sum==n) {
cout << n << " " << "its factors are" << " ";
for (vector<int>::iterator it = factor.begin(); it != factor.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
}
int main(){
int n;
cin >> n;
for (int a = 2; a <= n; a++) {
if (a % 2 == 0) {
cheaknum(a);
}
}
}
0.0分
1 人评分
帮我看看,我这个问题出在哪呢 #include<iostream> using namespace std; #include<vector> int main() { vector<int> v; int n; cin >> n; for(int i = 6; i<=n;i++) { int sum = 0; for(int j = 1; j<=i/2;j++) { if(i%j ==0) { v.push_back(j); } } for(vector<int>::iterator it = v.begin();it !=v.end();it++ ) { sum += *it; } if(sum == i) { cout << i << "its factors are"<< " "; for(vector<int>::iterator it = v.begin();it !=v.end();it++ ) { c
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:608 |
WU-蓝桥杯算法提高VIP-交换Easy (C++代码)浏览:1107 |
WU-蓝桥杯算法提高VIP-勾股数 (C++代码)浏览:1592 |
C语言程序设计教程(第三版)课后习题8.2 (C语言代码)浏览:5227 |
C语言程序设计教程(第三版)课后习题10.4 (C语言代码)浏览:879 |
母牛的故事 (C语言代码)浏览:715 |
1011题解浏览:760 |
C语言程序设计教程(第三版)课后习题9.10 (C语言代码)浏览:819 |
A+B for Input-Output Practice (IV) (C语言代码)浏览:484 |
简单的a+b (C语言代码)浏览:415 |
睡不醒 2023-04-14 21:49:45 |
out << *it << " "; } cout<<endl; } v.clear(); } return 0; }