Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Bitnami MediaWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
2556 - HN
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Enunţ== Fie N un număr natural și expresia HN=1+1/2+1/3+…+1/N. ==Cerința== Determinați numerele naturale P și Q ce reprezintă numărătorul respectiv numitorul fracției ireductibile HN=P/Q. ==Date de intrare== Fișierul de intrare hn.in conţine pe prima linie numărul natural N. ==Date de ieșire== Fișierul de ieșire hn.out va conţine pe prima linie numărul P și pe a doua linie numărul Q. ==Restricții și precizări== *2 ≤ N ≤ 10000 ==Exemplul 1== ;hn.in :6 ;hn.out :49 :20 ==Exemplul 2== ;hn.in :20 ;hn.out :55835135 :15519504 ==Exemplul 3== ;hn.in :-10 ;hn.out :Date de intrare invalide! ==Rezolvare== <syntaxhighlight lang="python" line> #2556 hn def gcd(a, b): while b: a, b = b, a % b return a def simplify_fraction(num, denom): divisor = gcd(num, denom) return num // divisor, denom // divisor def calculate_harmonic_fraction(N): numerator = 0 denominator = 1 for i in range(1, N + 1): numerator = numerator * i + denominator denominator *= i return numerator, denominator def validate_input(N): if not (isinstance(N, int) and 2 <= N <= 10000): with open('hn.out', 'w') as f: f.write("Date de intrare invalide!") return False return True def main(): try: # Citirea datelor din fișierul de intrare with open('hn.in', 'r') as f: N = int(f.readline().strip()) except FileNotFoundError: with open('hn.out', 'w') as f: f.write("Fisierul de intrare nu a fost gasit.") return except ValueError: with open('hn.out', 'w') as f: f.write("Date de intrare invalide!") return # Validarea datelor de intrare if not validate_input(N): return # Calcularea fractiei armonice si simplificarea ei numerator, denominator = calculate_harmonic_fraction(N) numerator, denominator = simplify_fraction(numerator, denominator) # Scrierea rezultatului in fisierul de iesire with open('hn.out', 'w') as f: f.write(str(numerator) + '\n') f.write(str(denominator)) if __name__ == "__main__": main() </syntaxhighlight>
Summary:
Please note that all contributions to Bitnami MediaWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Bitnami MediaWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width