第二周HDU-2010题解

问题链接:HDU-2010

问题简述

输入多组数据,每组数据包含两个整数n,m(100<=n<=m<=999),从小到大输出n到m之间的水仙花数(“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身),每个水仙花数之间以空格隔开,如果没有,输出“no”。

思路

运用for循环,寻找n到m之间所有的水仙花数,然后放入一个数组中,按照题目要求输出。

AC通过的C++语言程序如下:

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int main()
{
	int n, m;
	while (cin >> n >> m)
	{
		vector<int> a;
		for (int i = n; i <= m; i++)
			if (i == pow(i / 100, 3) + pow((i / 10) % 10, 3) + pow(i % 10, 3)) a.push_back(i);
		if (a.size() != 0)
		{
			for (int i = 0; i < a.size() - 1; i++)
				cout << a[i] << " ";
			cout << a[a.size() - 1] << endl;
		}
		else cout << "no" << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43970556/article/details/84994805