C++ Practice Problem – ‘Narcissistic Number’ Problem 2

Time Limit: 2s Memory Limit: 192MB

Problem Description

Output all “Narcissistic Numbers”. A “Narcissistic Number” is a three-digit number such that the sum of the cubes of its digits equals the number itself. For example: 371 is a “Narcissistic Number” because 371 = 3^3 + 7^3 + 1^3.

Input Format

None

Output Format

Output all “Narcissistic Numbers” (in ascending order, one per line)

Sample Input

None

Sample Output

None

Code

#include <iostream>
using namespace std;
int main() {
    for (int num = 100; num <= 999; ++num) {
        int a = num / 100;       // Hundreds place
        int b = (num / 10) % 10; // Tens place
        int c = num % 10;        // Units place
        if (a*a*a + b*b*b + c*c*c == num) {
            cout << num << endl;
        }
    }
    return 0;
}

Output Result

C++ Practice Problem - 'Narcissistic Number' Problem 2Iterate through all three-digit numbers.

Key Points of the Program:

  • Use a for loop to iterate through the number range
  • Mathematical method for digit decomposition
  • Optimized implementation of cube calculation
  • Conditional judgment and output format control
  • Program efficiency analysis

The above code is relatively simple and will not be explained in detail! Interested students can write one themselves.C++ Practice Problem - 'Narcissistic Number' Problem 2

C++ Basic Tutorial Collection

C++ Practice Problem - 'Narcissistic Number' Problem 2C++ Basic Materials

1. C++ Output

2. C++ Variables

3. C++ Input

4. C++ Expressions

5. IF Statements

6. IF Applications

7. WHILE Loop Statements

8. FOR Loop Statements

9. Arrays

10. One-Dimensional Arrays

11. Two-Dimensional Arrays

12. C++ Functions

13. C++ File Operations – Writing Files

If you find it interesting, just click on me

C++ Practice Problem - 'Narcissistic Number' Problem 2C++ Practice Problem - 'Narcissistic Number' Problem 2C++ Practice Problem - 'Narcissistic Number' Problem 2C++ Practice Problem - 'Narcissistic Number' Problem 2

Leave a Comment