⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 水仙花.cpp

📁 C++Example实用的算法:包括枚举
💻 CPP
字号:
//水仙花数算法
#include<iostream.h>
#include<math.h>
#include<time.h>
//函数声明
int arm_number(int max); 
//此函数的功能是求小于max的所有水仙花数
int main()
{
	int max;
	int second;
	time_t t;
	cout<<"input the max number: ";
	cin>>max;

	second=time(&t);
	arm_number(max);
	second=time(&t)-second;
	cout<<"The total times is:"<<second<<'\n';

	return 0;
}
//函数定义
int arm_number(int max) 
{
	int num;
	int radix=10;
	int x;
	int y;
	int time=0;
	for(num=1;num<=max;num++)
	{ 
                 //从1到max逐个搜索符合条件的数
		int n=0;
		int power=1;
		int total=0;
	while(power<=num)
	{
		power=power*radix;
		n++; //统计此数是几位数
		time++;
	}

	while(power>=10)
	{
		x=(num%power)/(power/10);
                                 //从高位到低位逐位求值
		y=n; //y为n次幂
		total=total+int(pow(x,y));
                                 //求每位数字的n次幂的和
		power=power/10;
		time++;
	}
	if(total==num) 
		cout<<"The arm number is: "<<num<<'\n'; 
                                //此数符合水仙数则输出 
	}
	cout<<"The times of computed is: "<<time<<'\n'; 
                                //统计计算机的运算次数
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -