#include
using namespace std;
int main()
{
int a=6,c=1,sum=0;
while (a<1000)
{
while (c<a)
{//验证某数是否为完全数。
if (a%c==0)
{
sum=sum+c;
}
c++;
}
if (sum==a)//如果是
{
cout <<a <<"=";
while (c<a)
{
if (a%c==0&&c<a)
{
cout <<c <<"+";
}
else
{
if (a==c)
{
cout <<c;
}
}
c=c+1;
}
}
a++;
}
return 0;
}
因为你自从第一个while后就没在每个while前对c赋初值,导致一直是c>=a的状态,所以后面的while就都没执行。
我把剩余代码简化了一下,while都换成了for,防止忘记赋初值,供参考,最后多余的加号通过退格('\b')删掉了
#include <iostream>
using namespace std;
int main() {
for( int a = 6; a < 1000; a++) {
int sum = 0;
for( int c = 1; c < a; c++ )//验证某数是否为完全数。
if ( a % c == 0 )
sum += c;
if ( sum == a ) {
cout << a << " = ";
for( int c = 1; c < a; c++ ) {
if ( a % c == 0 )
cout << c << " + ";
else if (a==c)
cout <<c;
}
cout << "\b\b " << endl;
}
}
return 0;
}
#include <iostream>
using namespace std;
using namespace std;
int main()
{
int a=6,c=1,sum=0;
while (a<1000)
{
sum = 0;
c = 1;
int cnt = 0;
while (c<a)
{
//验证某数是否为完全数。
if (a%c==0)
{
sum=sum+c;
cnt++;
}
c++;
}
if (sum==a)//如果是
{
cout << endl << a <<"=";
c = 1;
int cnt1 = 0;
while (c<a)
{
if (a%c==0)
{
cnt1++;
if (cnt != cnt1)
{
cout <<c <<"+";
}
else
{
cout <<c;
}
}
c=c+1;
}
}
a++;
}
return 0;
}
6=1+2+3
28=1+2+4+7+14
496=1+2+4+8+16+31+62+124+248