Se citeste un numar natural nenul. Sa se construiasca:
a) Cel mai mare numar care se poate forma din cifrele numarului citit luate o singura data.
b) Cel mai mare numar care se poate forma din cifrele numarului citit luate de cate ori apar
c) Cel mai mic numar care se poate forma din cifrele numarului citit luate o singura data
d) Cel mai mic numar care se poate forma din cifrele numarului citit luate de cate ori apar
n=32092075
a) x1=975320
b) x2=97532200
c) x3=203579
d) x4=20023579
Răspunsuri la întrebare
a)
def read_number():
while True:
try:
number = int(input("Enter number: "))
return number
except ValueError:
print("That's not a number. \nLet's try again. \n")
number = read_number()
digits = list()
for digit in str(number):
if int(digit) not in digits:
digits.append(int(digit))
digits.sort(reverse=True)
zero = 0
while digits[zero] == 0:
zero += 1
index = zero + 1
big_number = digits[zero]
while zero:
big_number *= 10
zero -= 1
while index < len(digits):
big_number = big_number * 10 + digits[index]
index += 1
print(big_number)
b)
def read_number():
while True:
try:
number = int(input("Enter number: "))
return number
except ValueError:
print("That's not a number. \nLet's try again. \n")
number = read_number()
digits = list()
for digit in str(number):
digits.append(int(digit))
digits.sort(reverse=True)
zero = 0
while digits[zero] == 0:
zero += 1
index = zero + 1
big_number = digits[zero]
while zero:
big_number *= 10
zero -= 1
while index < len(digits):
big_number = big_number * 10 + digits[index]
index += 1
print(big_number)
c)
def read_number():
while True:
try:
number = int(input("Enter number: "))
return number
except ValueError:
print("That's not a number. \nLet's try again. \n")
number = read_number()
digits = list()
for digit in str(number):
digits.append(int(digit))
digits.sort()
zero = 0
while digits[zero] == 0:
zero += 1
index = zero + 1
big_number = digits[zero]
while zero:
big_number *= 10
zero -= 1
while index < len(digits):
big_number = big_number * 10 + digits[index]
index += 1
print(big_number)
d)
def read_number():
while True:
try:
number = int(input("Enter number: "))
return number
except ValueError:
print("That's not a number. \nLet's try again. \n")
number = read_number()
digits = list()
for digit in str(number):
if int(digit) not in digits:
digits.append(int(digit))
digits.sort()
zero = 0
while digits[zero] == 0:
zero += 1
index = zero + 1
big_number = digits[zero]
while zero:
big_number *= 10
zero -= 1
while index < len(digits):
big_number = big_number * 10 + digits[index]
index += 1
print(big_number)
Am folosit acelasi algoritm pentru toate cerintele, cu modificari minuscule.
Cred ca poti observa ca diferenta dintre varianta cu cifrele "luate o singura data" si varianta cu "luate de cate ori apar" este lipsa, respectiv prezenta liniei 13, "if int(digit) not in digits_list:", iar diferenta dintre ordinea cifrelor este variabila "reverse" a listei: daca e True numarul e maxim, altfel e minim.