Home > Back-end >  This code where wrong, I can not find, infinite loop, who can help me
This code where wrong, I can not find, infinite loop, who can help me

Time:03-28

To find the number of c + +

#include
using namespace std;

Int main () {
Int I, j, k, l, a, [10].
for (i=2; i <=1000; I++) {
int sum=0;
K=0;
L=0;

For (j=1; J & lt; i; J++) {

If (I % j==0) {
The sum +=j;
A [k]=j;
k++;
L++;
}
}

If (sum==I) {
Cout & lt; for (i=0; i Cout & lt; Cout & lt; }
}


return 0;
}

CodePudding user response:

for (i=2; i <=1000; I++) {
.
for (I=0; i Cout & lt;

}
Use the same variable I do control

CodePudding user response:

reference 1st floor gouyanfen response:
for (I=2; i <=1000; I++) {
.
for (I=0; i Cout & lt;

}
Use the same variable I do control

Changed, then why can't the PTA also showed a runtime error

CodePudding user response:

Modified as follows, for your reference:
//c + + to find the number of 



#include

using namespace std;

Int main ()
{
Int I, j, k, l, sum;//a [10]
for (i=2; i <=1000; I++) {
Sum=0; K=0;//l=0;
For (j=1; J & lt; i; J++) {
If (I % j==0) {
The sum +=j;
//a [k++]=j;
//k++;//l++;
}
}
If (sum==I) {
Cout & lt; For (l=1; LIf (I % l==0)//for (I=0; i Cout & lt; Cout & lt; }
}

return 0;
}

CodePudding user response:

A little under the optimized:
//c + + to find the number of 

#include

using namespace std;

Int main ()
{
Int I, j, l, sum;
for (i=2; i <=1000; I++) {
Sum=0;
For (j=1; J & lt;=I/2; J++) {
If (I % j==0)
The sum +=j;
}
If (sum==I) {
Cout & lt; For (l=1; L & lt;=I/2; L++)
If (I % l==0)
Cout & lt; Cout & lt; }
}

return 0;
}
  • Related