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

Python daily practice - day 1: daffodils

編輯:Python

1.  Problem description

<font color=green size=4> Narcissus number is also called super perfect number invariant 、 Narcissistic number 、 Self idempotent 、 Armstrong number or Armstrong 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 .</font><br /><font color=black size=4>1、 The Narcissus count is a three digit number :111 333 456 999 5302、 The number of daffodils 、 ten 、 The sum of the hundreds of digit cubes is equal to the original number </font><br /><font color=#0099ff size=4> After the partners read the problem description , Be sure to practice yourself first , Then go to see the blogger's code and problem-solving ideas , In order to improve their programming level , It all depends on self-consciousness !!!

2.  Problem analysis

<font color=red size=4> How to get a bit 、 ten 、 The number in the hundreds ?</font><br />
  • <font color=black size=4> Single digit 37==1== : It's the original number pair 10 The result of the remainder operation  37==1== % 10 = ==1==<br />
  • <font color=black size=4> Ten digits 3==7==1:3==7==1 By dividing by 10, Can be 7 Move to a position ( Integers ):3==7==1 // 10 = 3==7==3==7==:3==7== Yes 10 Take the remainder to get the value of the last digit ==7== :3==7== % 10 = ==7==<br />
  • <font color=black size=4> Hundreds of digits ==3==71:==3== Is the original number divided by 100 Result ( to be divisible by ):==3==71 // 100 = ==3==<br />
  • <font color=black size=4> reflection : How to find the value at the specified position of any number ?1234==5==6789: First 10000 obtain 1234==5==; Right again 10 Get surplus ==5==

3.  Algorithm ideas

<font color=black size=4>1.  Use cycle from 100 Start to 999 end <br /><font color=black size=4>2.  Every time the circulation inside the body , Get a hundred digit number 、 Ten digit number 、 Single digit numbers <br /><font color=black size=4>3.  Judge whether the third power of a hundred digit number plus the third power of a ten digit number plus the third power of a single digit number is equal to itself , If so, it is the number of daffodils .

4.  Code implementation

for Loop code implementation

Implementation code :
print('100 To 1000 The number of all daffodils before is as follows :')
for i in range(100, 1000):
 #  Take a hundred  371 // 100 = 3
 x = i // 100
 #  Take the tens  371 // 10 =3 7; 37 % 10 = 7
 y = i // 10 % 10
 #  Take a single  371 % 10 = 1
 z = i % 10
 #  Judge the position 、 ten 、 The sum of the hundreds of digit cubes is equal to the original number
 if x ** 3 + y ** 3 + z ** 3 == i:
 print(f'{i} It's Narcissus ')

Running results :

while Loop code implementation

Implementation code :
print('100 To 1000 The number of all daffodils before is as follows :')
number = 100
while number < 1000:
 #  Take a hundred  371 // 100 = 3
 x = number // 100
 #  Take the tens  371 // 10 =3 7; 37 % 10 = 7
 y = number // 10 % 10
 #  Take a single  371 % 10 = 1
 z = number % 10
 #  Judge the position 、 ten 、 The sum of the hundreds of digit cubes is equal to the original number
 if x ** 3 + y ** 3 + z ** 3 == number:
 print(f'{number} It's Narcissus ')
 #  Need to set up number Add one... At a time
 number += 1

Running results :

5.  How to make question brushing more efficient ?

<font color=black size=5>1.  Programming white contestant </font></br><font color=black size=3> Many novice programmers have learned basic grammar , But I don't know the purpose of grammar , I don't know how to deepen the image , I don't know how to improve myself , This is the time <font color=red size=3> It's very important to brush one question independently every day ( Refining into a God ), You can go to the introductory training of programming beginners on Niuke online .</font> This topic is at the entry level of programming , Suitable for Xiaobai who has just learned grammar , The topic involves basic grammar of programming , Basic structure, etc , Each question has practice mode and examination mode , The test mode can be restored for simulation , You can also practice through practice mode .</font></br> Link address : Cattle from  |  Beginner programming training <font color=black size=5>2.  Advanced programming player </font></br><font color=black size=3> When you have gradually mastered the key points of knowledge after basic practice , Go to this time <font color=red size=3> Learn data structures in special exercises 、 Algorithm basis 、 Fundamentals of computer </font> etc. . Start with the simple , If you feel up, do it in medium difficulty , And more difficult topics .<font color=red size=3> These three are the knowledge points that must be tested in the interview </font>, We can only insist on practicing more every day by ourselves , Refuse to lie flat and continue to brush questions , Continuously improve yourself to impact a satisfactory company .</font></br> Link address : Cattle from  |  Special exercises <font color=black size=3> Speed up , Let's attack the big factory together , If you have questions, leave a message in the comment area to answer !!!
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved