输出距离一个整数最近的水仙花数

输入一个整数N,输出距离整数N最近的一个水仙花数。

通过判断输入的整数距离哪一个水仙花数距离最近,来输出对应的水仙花数

# include <iostream>
#include <cmath>
#include <vector>
/*
输入一个整数判断距离这个整数最近的水仙花数 
*/ 
using namespace std;
int main(){
	int number;
	cin>>number;
	vector<int> temp = {153,370,371,407}; //水鲜花数 
	while(temp.size()>1){
		if(abs(number-temp.front()<=abs(number-temp.back()))){
			temp.pop_back();
		}
		else{
			temp.erase(temp.begin());
		}
	}
	cout<<temp[0]<<endl;
	return 0;
} 
发布了46 篇原创文章 · 获赞 19 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/li123_123_/article/details/100583613