Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
python cuantos movimientos para llegar a cero

He intentado hacer esta tarea de muchas maneras, pero ninguna funciona.

El usuario ingresa un número natural n. En un movimiento, su dígito más grande se resta del número. En el siguiente movimiento, su dígito más alto se resta del resultado, etc. El programa necesita determinar e imprimir cuántos movimientos se necesitan para llegar a cero. Por ejemplo, el número 24 requiere cinco movimientos (24 → 20 → 18 → 10 → 9 → 0).

Esto es lo que he hecho hasta ahora. Sé cómo encontrar el dígito más grande, pero ¿cómo puedo poner esto en un bucle para restar el dígito más grande del resultado?

 num = int(input("Enter natural number ")) if num <= 0: print("That is not a natural number") else: max = 0 while num > 0: digit = num % 10 if max < digit: max = digit num = num // 10 print("Largest Digit is : ", max)
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Podrías probar con una cadena y max :

 num = int(input("Enter natural number ")) if num <= 0: print("That is not a natural number") else: ntimes = 0 while num > 0: num -= int(max(str(num))) ntimes += 1 print(ntimes)

Producción:

 5
over 4 years ago · Santiago Trujillo Report

0

En caso de que tenga el requisito de no convertir el número en una cadena para obtener sus dígitos, puede usar divmod :

 def get_natural_num_input(prompt: str) -> int: num = -1 while True: try: num = int(input(prompt)) if num <= 0: print("Error: Not a natural number, try again...") else: break except ValueError: print("Error: Enter a integer, try again...") return num def get_digits(num: int) -> list[int]: digits = [] while num > 0: num, digit = divmod(num, 10) digits.append(digit) return digits def moves_to_get_zero(num: int) -> int: moves = 0 while num != 0: num -= max(get_digits(num)) moves += 1 return moves num = get_natural_num_input("Enter a natural number: ") print(f'moves_to_get_zero({num}) = {moves_to_get_zero(num)}')

Ejemplo de uso:

 Enter a natural number: 24 moves_to_get_zero(24) = 5
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!