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

Python exercise 4- number of flowers in water

編輯:Python

subject : Output narcissus number ( The number of Narcissus refers to a 3 digit , Of the numbers in each of its bits 3 The sum of the powers is equal to itself ( for example :1^3 + 5^3+ 3^3 = 153))

for i in range (1,10): # Set up i The value range of is 1-9
for j in range (0,10): # Set up j The value range of is 0-9
for k in range (0,10): # Set up k The value range of is 0-9
if i*100+j*10+k == i**3+j**3+k**3: # Judge i ride 100+j ride 10+k Is it equal to i Of 3 The next power +j Of 3 The next power +k Of 3 The next power If equal, go to the subcode
print(" Narcissus count :%s"%(i*100+j*10+k)) Print i ride 100+j ride 10+k Value The result is :
Narcissus count :153
Narcissus count :370
Narcissus count :371
Narcissus count :407

Their thinking : Define a three digit number ijk(i For hundreds ,j For ten ,k For a bit ), therefore i ride 100+j ride 10+k It's a three digit number , Then make the whole three digits equal i The third power of +j The third power of +k The third power of is OK , At this time, we can use traversal to count a group of numbers , If it matches the result, print it out .

  Sort out this topic for The general flow of the cycle :

When i be equal to 1 when , The code goes to the following j Of for loop , here i=1 j=0  Then go down k Of for In circulation , here i =1 j=0 k=0 And then go on if Judge ,100 Does not meet the results , Then back k Of for In circulation , continue k Of for loop , here i=1 j=0 k=1, then if Judge non conformity , Keep going k Of for loop , until i=1 j=0 k=9 Find out k Of for The cycle is over , Then return to j Of for In circulation , obtain i =1 j=1 , Then continue down k Of for In circulation , here i=1 j=1 k=0, Then continue until i=1,j=1,k=9 , Back to j Of for In circulation , here i=1 j=2 Then go down k Of for loop , here i=1 j=2 k=0, Just keep walking , until i=1 j=9 k=9 after , Back to i Of for In circulation , here i=2, Then enter and go down j The cycle of i=2 j=0 And then go down there k At this time in the cycle of i=2 j=0 k=0, Just keep walking , Finally, until i=9 j=9 k=9, I found that all the numbers have been traversed , end for Loop traversal . So you can 100-999 Try all the numbers again , Then print out the number that matches the daffodil .

Simply sort it out : First, when i=1 when , Because the subcode has j The traversal , So first from j=0 Start , then j And then there is k The traversal , So when you go from k=0 To k=9 After traversing , Back to j Continue in the traversal of j The traversal , Then it's time to j=1 when ,k From the 0 To 9 The traversal , And then back to j, until j=9 And finish j=9 when k From the 0 To 9 The traversal , Back to i Traversal of , Conduct i=2, And then back to j The traversal , And then we can move on j and k From the 0 To 9 The traversal , Then return to i Traversal of , until i=9 when ,j and k Of 0 To 9 End of traversal , End of cycle . If you print step by step, it is from 100 Print sequentially to 999.


  The official account is two-dimensional code. , The content is sent out synchronously , We can focus on learning together

                                                                                               

This is the official account of Zhang Gouzi's brother brother. , Will share some of the usual work experience , You can pay attention to .

                                                                                                


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