13 条题解

  • 0
    @ 2022-12-20 21:45:04

    水题。 AC代码:

    #include<iostream>
    #include<math.h>
    using namespace std;
    int m,n;
    bool check(int x){//自定义函数
    	int s=0,y=x;
    	while(y!=0){
    		s+=pow(y%10,3);//求每个数位上的数的立方
    		y/=10;//删除当前数位
    	}
    	if(s==x)return true;//是水仙花数
    	else return false;//不是水仙花数
    }
    int main(){
    	int sum;
    	cin>>m>>n;
    	for(int i=m;i<=n;i++){
    		if(check(i)){//引用自定义函数
    			cout<<i<<" ";
    			sum++;
    		}
    	}
    	if(sum>0)cout<<endl<<sum;//有水仙花数
    	else cout<<"No Answer.";//没有水仙花数
    	return 0;
    }
    

    无注释纯净版:

    #include<iostream>
    #include<math.h>
    using namespace std;
    int m,n;
    bool check(int x){
    	int s=0,y=x;
    	while(y!=0){
    		s+=pow(y%10,3);
    		y/=10;
    	}
    	if(s==x)return true;
    	else return false;
    }
    int main(){
    	int sum;
    	cin>>m>>n;
    	for(int i=m;i<=n;i++){
    		if(check(i)){
    			cout<<i<<" ";
    			sum++;
    		}
    	}
    	if(sum>0)cout<<endl<<sum;
    	else cout<<"No Answer.";
    	return 0;
    }
    

    可能或许大概稍微写得难了一点点。。。点个赞再走吧!

    信息

    ID
    898
    时间
    1000ms
    内存
    128MiB
    难度
    6
    标签
    递交数
    1403
    已通过
    410
    上传者