2013-05: Small 1
Write a program that finds all the numbers less than 1000 that contain only digits that sum to 10, e.g. 55, 73, 137, but not 423.
How many are there?2012-11: Math 1
Find all the numbers less than 1000, where the sum of the digits is 15, for example 96 or 177.
How many are there?
(…)
Scrivi un programma che trova tutti i numeri minori di 1000 la cui somma delle cifre sia uguale a 10 (15).
Quanti sono?
Soluzione
La somma delle cifre per i numeri minori di 100
For numero=1 to 99 ' fino a 99... decine=Math.Floor(numero/10) ' 1° cifra unità =Math.Remainder(numero, 10) ' 2° cifra somma =unità+decine TextWindow.WriteLine(numero + ": " + somma) EndFor
La somma delle cifre per i numeri minori di 1000
For numero=1 to 999 ' fino a 999... centinaia=Math.Floor(numero/100) ' 1° cifra x =Math.Remainder(numero, 100) ' 2* e 3° cifra... decine =Math.Floor(x/10) ' 2° cifra unità =Math.Remainder(numero, 10) ' 3° cifra somma =unità+decine+centinaia TextWindow.WriteLine(numero + ": " + somma) EndFor
I numeri con una certa somma delle cifre
magic_number=10 ' oppure 15
For numero=1 to 999
centinaia=Math.Floor(numero/100)
decine =Math.Floor(Math.Remainder(numero, 100)/10)
unità =Math.Remainder(numero, 10)
somma =unità+decine+centinaia
If(somma = magic_number) Then ' se la somma è quella giusta...
TextWindow.Write(numero + " ")
EndIf
EndFor
Quanti sono?
magic_number=10 ' oppure 15
quanti =0 ' finora nessuno...
For numero=1 to 999
centinaia=Math.Floor(numero/100)
decine =Math.Floor(Math.Remainder(numero, 100)/10)
unità =Math.Remainder(numero, 10)
somma =unità+decine+centinaia
If(somma = magic_number) Then
TextWindow.Write(numero + " ")
quanti=quanti+1 ' uno in più
EndIf
EndFor
TextWindow.WriteLine("")
TextWindow.WriteLine(quanti + " numeri!") ' quanti sono?