判断水仙花数并输出,c++

以下是使用C++实现的相同逻辑代码:

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
#include <cmath>

// 判断一个数是否为水仙花数
bool isNarcissistic(int n) {
    std::string numStr = std::to_string(n);
    int numDigits = numStr.length();
    int sum = 0;
    
    // 遍历每一位数字
    for (int i = 0; i < numDigits; i++) {
        // 将字符数字转换为整数
        int digit = numStr[i] - '0';
        // 计算该位数字的 n 次幂并累加
        sum += static_cast<int>(std::pow(digit, numDigits));
    }
    
    // 判断总和是否等于原数
    return sum == n;
}

// 查找指定范围内的水仙花数
std::vector<int> findNarcissisticNumbers(int min, int max) {
    std::vector<int> results;
    for (int i = min; i <= max; i++) {
        if (isNarcissistic(i)) {
            results.push_back(i);
        }
    }
    return results;
}

int main() {
    // 输出所有3位水仙花数(100-999)
    std::vector<int> narcissisticNumbers = findNarcissisticNumbers(100, 999);
    
    // 格式化输出
    if (!narcissisticNumbers.empty()) {
        std::cout << "3位水仙花数有:" << std::endl;
        for (int num : narcissisticNumbers) {
            std::cout << num << std::endl;
        }
    } else {
        std::cout << "该范围内没有水仙花数" << std::endl;
    }
    
    return 0;
}

以下是使用纯数学运算(不用字符串转换)的替代版本:

cpp 复制代码
#include <iostream>
#include <vector>

// 判断一个数是否为水仙花数(纯数学方法)
bool isNarcissistic(int n) {
    int original = n;
    int numDigits = 0;
    int temp = n;
    
    // 计算位数
    while (temp > 0) {
        numDigits++;
        temp /= 10;
    }
    
    int sum = 0;
    temp = original;
    
    // 遍历每一位数字
    while (temp > 0) {
        int digit = temp % 10;
        // 计算digit的numDigits次幂
        int power = 1;
        for (int i = 0; i < numDigits; i++) {
            power *= digit;
        }
        sum += power;
        temp /= 10;
    }
    
    // 判断总和是否等于原数
    return sum == original;
}

// 查找指定范围内的水仙花数
std::vector<int> findNarcissisticNumbers(int min, int max) {
    std::vector<int> results;
    for (int i = min; i <= max; i++) {
        if (isNarcissistic(i)) {
            results.push_back(i);
        }
    }
    return results;
}

int main() {
    // 输出所有3位水仙花数(100-999)
    std::vector<int> narcissisticNumbers = findNarcissisticNumbers(100, 999);
    
    // 格式化输出
    if (!narcissisticNumbers.empty()) {
        std::cout << "3位水仙花数有:" << std::endl;
        for (int num : narcissisticNumbers) {
            std::cout << num << std::endl;
        }
    } else {
        std::cout << "该范围内没有水仙花数" << std::endl;
    }
    
    return 0;
}

两个版本的功能相同,都能正确找到153、370、371、407这4个3位水仙花数。第一个版本更接近原Lua代码的逻辑,使用字符串处理;第二个版本使用纯数学运算,在某些情况下性能更好。

相关推荐
努力学算法的蒟蒻1 天前
day63(1.22)——leetcode面试经典150
算法·leetcode·面试
梵尔纳多1 天前
第一个 3D 图像
c++·图形渲染·opengl
永远都不秃头的程序员(互关)1 天前
【决策树深度探索(一)】从零搭建:机器学习的“智慧之树”——决策树分类算法!
算法·决策树·机器学习
xiaoqider1 天前
C++继承
开发语言·c++
YE1234567_1 天前
从底层零拷贝到分布式架构:深度剖析现代 C++ 构建超大规模高性能 AI 插件引擎的实战之道
c++·分布式·架构
程序员-King.1 天前
day161—动态规划—最长递增子序列(LeetCode-300)
算法·leetcode·深度优先·动态规划·递归
西柚小萌新1 天前
【计算机视觉CV:目标检测】--3.算法原理(SPPNet、Fast R-CNN、Faster R-CNN)
算法·目标检测·计算机视觉
高频交易dragon1 天前
Hawkes LOB Market从论文到生产
人工智能·算法·金融
脏脏a1 天前
C++ 容器的两把利器:优先级队列与反向迭代器
c++·反向迭代器·优先级队列
张张努力变强1 天前
C++ 类和对象(三):拷贝构造函数与赋值运算符重载之核心实现
开发语言·c++