| |

VerySource

 Forgot password?
 Register
Search
View: 1624|Reply: 11

C ++ finished number

[Copy link]

3

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-6 14:30:01
| Show all posts |Read mode
Program to find all "complete numbers" within 500. The so-called "complete number" is a number that is exactly equal to the sum of all its different factors (including 1), and the cubic sum of its digits is equal to the number itself. Example: 6 = 1 + 2 + 3


The result is: 1 + 2 + 3 = 6 1 + 2 + 3 + 4 + 7 + 14 = 28 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 = 496

How to make it? ? ? ? ? ?
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-6 18:18:01
| Show all posts
#include <iostream>
#include <cmath>
using namespace std;

int main () {
for (int i = 2; i <= 500; ++ i) {
int sum = 0;
for (int j = 1; j <= sqrt ((double) i); ++ j) {
if (0 == i% j) {
sum + = j;
int j2 = i / j;
if (j2! = j&&j2! = i)
sum + = j2;
}
}
if (sum == i) {
cout << i << endl;
}
}
}
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-11 15:45:01
| Show all posts
I have to ask what the header file "#include <cmath>" is for! ! ?
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-7-11 21:15:01
| Show all posts
cmath is the usage of the new C++ standard library, and math.h is the past usage.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-7-15 21:30:01
| Show all posts
Because I use the sqrt function, and it is declared in cmath.
Reply

Use magic Report

1

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-7-29 14:30:01
| Show all posts
int wanshu(int k,int num,int sum)
{
if(sum==num&&k==num) return 1;
else{
if(m/k==0)
sum+=k;
return wanshu(k+1,m,sum);
}
return 0;
}
wanshu(1,i,0);
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 India

Post time: 2020-7-30 21:30:01
| Show all posts
Talk about the algorithm
I don't understand math
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 Invalid IP Address

Post time: 2020-8-1 12:30:01
| Show all posts
What's the point? The algorithm of this program is the simplest and most straightforward: find out all the factors of each number, and then add them up to see if it is this number. If it is, it means it is a complete number. ——This is not the subject itself.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 United States

Post time: 2020-8-4 16:00:03
| Show all posts
[code] 1 #include<iostream>
      2 using namespace std;
      3 int main()
      4 {
      5 int i=1,j=1,sum=0;
      6 for(i=1;i<=500;i++)
      7 {
      8 sum=0;
      9 for(j=1;j<i;j++)
     10 {
     11 if((i%j)==0)
     12 sum=sum+j; //Judge whether it is a complete number
     13}
     14 if(sum==i) //If it is the output result
     15 {
     16 cout<<endl<<i<<"is Wan Bei Shu!"<<endl;
     17 for(j=1;j<i;j++)//output all divisors
     18 {
     19 if(i%j==0)
     20 cout<<" "<<j<<" ";
     twenty one             }
     twenty two         }
     twenty three     }
     24 cout<<endl;
     25 return 0;
     26}
[/code]
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-23 22:45:01
| Show all posts
#include <iostream>
using namespace std;

int main()
{
int n;
int sum;
for(n=1;n<=500;n++)
{int sum=0;
for(int i=1;i<n;i++)
{
if(n%i==0)
sum+=i;
}
if(sum==n)
cout<<n<<endl;
}
return 0;
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list