Python Nozioni di baseFacile

Conversione da decimale a esadecimale

Guida dettagliata e implementazione Python per il problema "Conversione da decimale a esadecimale".

Dichiarazione del problema

Facile

Scrivi una funzione decimal_to_hex(n) che accetta un intero non negativo n e restituisce una stringa che rappresenta il suo equivalente esadecimale (base 16) utilizzando lettere maiuscole (A-F). Non includere zeri iniziali (ad eccezione dell'input 0, che dovrebbe restituire "0").

Per convertire, dividere ripetutamente per 16 e raccogliere i resti (usando A=10, B=11, ..., F=15) in ordine inverso.

Vincoli
  • 0 <= n <= 10^6

Esempi

Example 1
Input
decimal_to_hex(255)
Output
'FF'
Explanation

255 ÷ 16 = 15 remainder 15. 15 = F. So result is FF.

Example 2
Input
decimal_to_hex(26)
Output
'1A'
Explanation

26 ÷ 16 = 1 remainder 10. 10 = A. Result: 1A.

Example 3
Input
decimal_to_hex(100)
Output
'64'
Explanation

100 ÷ 16 = 6 remainder 4. Result: 64.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Numbers come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Apri nell'editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.