c++中输出“水仙花数”

输出所有的水仙花数,所谓的“水仙花数”是指一个3位数,其各位数字立方和等于该数本身,例如153是一个水仙花数,因为153=1的立方+5的立方+3的立方

#include "stdafx.h"
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"水仙花数为:";
	int n,bw,sw,gw;//n代表一个三位数,bw表示百位数字,sw表示十位数字,gw表示个位数字
	for(n=100;n<1000;n++){
		bw=n/100;
		sw=(n-bw*100)/10;
		gw=n%10;
		if(n==bw*bw*bw+sw*sw*sw+gw*gw*gw)
			cout<<n<<"    ";

	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_43090158/article/details/82915886