在线编程——水仙花数(python&C++)

在线编程——水仙花数(python&C++)

题目地址:水仙花数——牛客网

水仙花数的数学定义:

        水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 n 位数(n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。

题目描述

春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的: “水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3。 现在要求输出所有在m和n范围内的水仙花数。

输入描述:

输入数据有多组,每组占一行,包括两个整数m和n(100 ≤ m ≤ n ≤ 999)。

输出描述:

对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。
示例1

输入

100 120
300 380

输出

no
370 371

python代码:

# coding = utf-8
import sys

def getWXFnumber(num1, num2):
    res = []
    if num1 < 0 or num2 > 999:
        return 0
    if num1 >= 0 and num2 <= 999:
        for i in range(num1, num2 + 1):
            tmp = []
            if len(str(i)) < 3 or len(str(i)) > 3:
                continue
            for j in range(len(str(i))):
                tmp.append(i // pow(10, j) % 10)
            getSum = pow(tmp[0], 3) + pow(tmp[1], 3) + pow(tmp[2], 3)
            if getSum == i:
                res.append(i)
            del tmp
    return res


if __name__ == '__main__':
    try:
        while True:
            arr1 = [int(t) for t in sys.stdin.readline().split()]
            res1 = getWXFnumber(arr1[0], arr1[1])
            if len(res1) >= 1:
                print(" ".join(str(i) for i in res1))
            if len(res1) == 0:
                print("no")
    except:
        pass

运行结果:


总结:

1、以上运行环境为python3,若为python2,则需要在代码开头添加

# coding:utf-8 #与 # coding = utf-8等价

2、输入的其他等价方法:

(1)python2:

# coding = utf-8
arr1 = [int(t) for t in raw_input("").split()]

(2)python3

arr1 = [int(t) for t in input("").split()]


C++代码1:

#include <iostream>    
#include <vector> 
#include <cmath>
using namespace std;

//求一个整数是几位数
int getNumberLength(int num){
	int length = 0;
	while (num){
		num /= 10;
		length++;
	}
	return length;
}

//求区间[lowIndex,highIndex]的水仙花数
vector<int> getWXFnumber(int lowIndex, int highIndex){
	vector<int> res;
	if (lowIndex < 0 || highIndex>999) return res;
	else if (lowIndex >= 0 && highIndex <= 999){
		for (int i = lowIndex; i < highIndex + 1; i++){
			vector<int> tmp;
			int len = getNumberLength(i);
			if (len != 3)//求一个整数的长度
				continue;
			for (int j = 0; j < len; j++){
				int val = i / pow(10, j);
				tmp.push_back(val % 10);
			}
			int sum = pow(tmp[0], 3) + pow(tmp[1], 3) + pow(tmp[2], 3);
			if (sum == i){
				res.push_back(i);
			}
			tmp.clear();
		}
		return res;
	}
}

int main(){
	vector<int> index(2),myRes;
	while (1){
		for (int i = 0; i < 2; i++){
			cin >> index[i];
		}
		myRes = getWXFnumber(index[0], index[1]);
		int len = myRes.size();// 求myRes长度  
		if (len >= 1) {
			for (int j = 0; j < len; j++){
				cout << myRes[j] << " ";
			}
			cout << endl;
		}
		else{
			cout << "no" << endl;
		}

	}
	system("pause");
	return 0;
}

运行结果:


但是提交结果:


经分析,主要问题在于主函数,因此需要改进,这里提供3个改进版本:

C++代码1改进版(仅改主函数):

#include <iostream>    
#include <vector> 
#include <cmath>
using namespace std;

//求一个整数是几位数
int getNumberLength(int num){
	int length = 0;
	while (num){
		num /= 10;
		length++;
	}
	return length;
}

//求区间[lowIndex,highIndex]的水仙花数
vector<int> getWXFnumber(int lowIndex, int highIndex){
	vector<int> res;
	if (lowIndex < 0 || highIndex>999) return res;
	else if (lowIndex >= 0 && highIndex <= 999){
		for (int i = lowIndex; i < highIndex + 1; i++){
			vector<int> tmp;
			int len = getNumberLength(i);
			if (len != 3)//求一个整数的长度
				continue;
			for (int j = 0; j < len; j++){
				int val = i / pow(10, j);
				tmp.push_back(val % 10);
			}
			int sum = pow(tmp[0], 3) + pow(tmp[1], 3) + pow(tmp[2], 3);
			if (sum == i){
				res.push_back(i);
			}
			tmp.clear();
		}
		return res;
	}
}

int main(){
	vector<int> myRes;
	int lowIndex, highIndex;
	while (cin >> lowIndex >> highIndex){
		myRes = getWXFnumber(lowIndex, highIndex);
		int len = myRes.size();// 求myRes长度  
		if (len >= 1) {
			for (int j = 0; j < len - 1; j++){
				cout << myRes[j] << " ";
			}
			cout << myRes[len - 1];
		}
		else if (len == 0){
			cout << "no";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

运行结果如上,提交结果:


C++代码1改进版(不使用getNumberLength函数):

#include <iostream>    
#include <vector> 
#include <cmath>
using namespace std;

//求区间[lowIndex,highIndex]的水仙花数
vector<int> getWXFnumber(int lowIndex, int highIndex){
	vector<int> res;
	if (lowIndex < 0 || highIndex>999) return res;
	else if (lowIndex >= 0 && highIndex <= 999){
		for (int i = lowIndex; i <= highIndex; i++){
			int tmp;
			int value = i;
			int sum = 0;
			while (value > 0){
				tmp = value % 10;
				sum += pow(tmp, 3);//sum += tmp*tmp*tmp;
				value /= 10;
			}
			if (sum == i){
				res.push_back(i);
			}
		}
		return res;
	}
}

int main(){
	vector<int> myRes;
	int lowIndex, highIndex;
	while (cin >> lowIndex >> highIndex){
		myRes = getWXFnumber(lowIndex, highIndex);
		int len = myRes.size();// 求myRes长度  
		if (len >= 1) {
			for (int j = 0; j < len - 1; j++){
				cout << myRes[j] << " ";
			}
			cout << myRes[len - 1];
		}

		else if (len == 0){
			cout << "no";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

运行结果如上,提交结果:


C++代码2(方法——利用isDaffy函数判断当前数是否为水仙花数):

#include <iostream>    
#include <vector> 
#include <cmath>
using namespace std;

//判断一个数是否为水仙花数
bool isDaffy(int num){
	int tmp;
	int sum = 0;
	int value = num;
	while (value > 0){
		tmp = value % 10;
		sum += pow(tmp, 3); //等价于 sum += tmp*tmp*tmp;
		value /= 10;
	}
	if (sum == num) 
		return true;
	else 
		return false;
}

int main(){
	int lowIndex, highIndex;
	while (cin>>lowIndex>>highIndex){
		int count = 0;
		for (int j = lowIndex; j <= highIndex; j++){
			if (isDaffy(j)){
				if (count == 0) 
					cout << j;
				else 
					cout << " " << j;
				count++;
			}
		}
		if (count == 0)
			cout << "no";
		cout << endl;
	}
	system("pause");
	return 0;
}

运行结果如上,提交结果:


总结:

1、C++代码1改进版的主函数中,若换成向量方式输入,运行不通过!!!

int main(){
	vector<int> Index(2),myRes;
	while (1){
		for (int i = 0; i < 2; i++){
			cin >> Index[i];
		}
		myRes = getWXFnumber(Index[0], Index[1]);
		int len = myRes.size();// 求myRes长度  
		if (len >= 1) {
			for (int j = 0; j < len - 1; j++){
				cout << myRes[j] << " ";
			}
			cout << myRes[len - 1];
		}

		else if (len == 0){
			cout << "no";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}
提交结果为:


2、输出格式一定要注意:

300 380

370 371(后面无空格)


参考网址:

1、输出列表元素,以空格或逗号为分隔符:https://blog.csdn.net/yangnianjinxin/article/details/77127257

2、https://blog.csdn.net/cityzenoldwang/article/details/78621337

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值