Andyhimself 2022-09-20 18:08 采纳率: 25%
浏览 29
已结题

c++水仙花数 不知道错在哪😩

img


#include
#include
using namespace std;

int main() {
int N;//N是位数
cin >> N;
for (int tot = pow(10, N - 1); tot < pow(10, N); tot++) {

    while (tot != 0) {
        int sum = 0;
        int temp;
        temp = tot % 10;
        tot = tot / 10;
        int squ = pow(temp, N);
        sum += squ;
    }
    if (sum == tot) {
        cout << tot << endl;

    }
}

}

return 0;
}

  • 写回答

2条回答 默认 最新

  • 亖夕 新星创作者: python技术领域 2022-09-20 18:18
    关注

    你应该sum定义为全局变量不然if语句中的sum无法被访问到,然后每一次for循环结束后再将sum赋值为0

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月28日
  • 已采纳回答 9月20日
  • 创建了问题 9月20日