The realization of Narcissus number is a relatively classical algorithm problem, today we first in VFP to achieve it.
First, let's take a look at what is "Narcissus number". The so-called Narcissus number refers to an n-digit number, its members of the digital Cube and equal to the number itself, such as: 153=13+53+33 , so 153 is a number of daffodils.
Let's do a simple one: solve the number of daffodils in the 3-digit number, that is, the number of daffodils between 100 and 999. Obviously this program needs to use loops, and from the concept of Narcissus number, the focus is to solve the current value of the loop variable values of the numbers. The rest of the work is simple, the figure of the solution to the cube and the current value of the loop variable comparison, if the equality is that this is a narcissus number, output it. See code comments for instructions. This article is published in the introductory web of programming: www.bianceng.cn
We use the form to implement this example, run the following figure:
Refer to the above figure to start our production:
Create a new form, add a Label control to the form, set the Caption property value to show the number of daffodils from 100 to 999, add two command buttons Command1 and Command2, and set their Caption property values to start and purge respectively ; Add an edit box control Edit1, and the property values are the default.
Second, add the event code:
1, "Clear" button click event:
Thisform.edit1.value= ""
Thisform.refresh
2, "Start" button click event:
The value of local i,a,b,c for i=100 to 999 a=int (i/100) && hundred equals this three-digit divided by 100 after rounding
B=int ((i-100*a)/10) & & Use this three-digit number minus its hundred number and 100 product,
&& to get the difference divided by 10 after the rounding, the result is 10-bit value
c=i-int (I/10) *10 && the same principle as above, This is a bit of value
if i=a^3+b^3+c^3 && set up instructions are narcissus number and output
thisform.edit1.value=thisform.edit1.value+str (i,5) +CHR && with Chr (13) line
endif
endfor
OK, run the form.