程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python to find the number of daffodils

編輯:Python

Narcissistic number , That is, the sum of the cubes of the numbers in each digit is equal to its own number .

example :153

1^3=1

5^3=27

3^3=125

1+125+27

=126+27

=153

= The original number

therefore ,153 It's a narcissus number .

-------------------------------------------

Code

First :

#coding=utf-8
a=str(input(" Numbers :"))

Get the number entered by the user , And turn it into numbers , Easy to traverse

secondly :

s=0
for i in a:
s+=int(i)**3 

Define a sum variable , Traversal string , Add the cube of each number

if s==int(a):
print(" yes ")
else:
print(" No ")

Finally, judge whether it is the same , As the same, the output is , Different output is not

Complete code

#coding=utf-8
a=str(input("input:"))
s=0
for i in a:
s+=int(i)**3
if s==int(a):
print(" yes ")
else:
print(" No ")

The way 【2】:

#coding=utf-8
a=int(input("number:"))

Get input number , And convert it to an integer

s=0
v=int(a) # Backup

First define the variables and as 0,a Define a backup ( Can't operate directly a, Otherwise, it cannot be compared )

for i in range(len(str(a))):
last=v%10
s+=last**3
v-=last
v/=10

Traverse by digits a Variable , Use the remainder to calculate the last digit , Add to the cube s, Then remove the last bit

notes :% It means to take the remainder , use v Subtract the remainder , Divided by 10, Equivalent to direct “ Wipe off ” Last digit

Finally, the comparison : Judge whether it is the same as the original number

if s==int(a):
print(" yes ")
else:
print(" No ")

complete :

#coding=utf-8
a=int(input("number:"))
s=0
v=int(a) # Backup
for i in range(len(str(a))):
last=v%10
s+=last**3
v-=last
v/=10
if s==int(a):
print(" yes ")
else:
print(" No ")

-------------------------------------- End ----------------------------------------------------------------------------------------


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved