Mündəricat
Problem bəyanat
“Seconds To Days” problemi sizə bir məlumat verildiyini bildirir tam saniyələrə istinad edir. Müvafiq gün, saat, dəqiqə və saniyə sayını çap edin.
misal
s = 1543765
17dd 20hh 49mm 25ss
İzahat: Verilən girişin tələb olunan formata çevrilməsi. 17 d, 20 h, 49 m və 25 s əldə edirik.
s = 9856743
114dd 1hh 59mm 3ss
Yanaşma
Aşağıdakı dəyişənləri hesablamaq üçün riyazi düsturdan istifadə edin -
Bildiyimiz kimi 1 gün = 24 * 3600 saniyə
Buna görə saniyələri dəqiqələrə çevirmək üçün -
numberOfDays = inputSeconds / 24 * 3600
Bildiyimiz kimi 1 saat = 3600 saniyə
Buna görə saniyələri dəqiqələrə çevirmək üçün -
numberOfHours = qalanSeconds / 3600
Bildiyimiz kimi 1 dəqiqə = 60 saniyə
Buna görə saniyələri dəqiqələrə çevirmək üçün -
sayıOfMinutes = remianingSaniyə / 60
Saniyələri günlərə çevirmək üçün alqoritm
1. Initialise a variable of interger type s referring to the seconds. 2. Create function to convert the seconds into the days, hours, minutes and seconds which accept an integer variable as it's parameter. 3. After that, create a variable of interger type days and initialise it's value as the result of the division of the given integer variable s and 24x3600. 4. Update the value of given integer variable s as the result of the mod of the given integer variable s and 24x3600. 5. Similarly, create a variable of interger type hours and initialise it's value as the result of the division of the given integer variable s and 3600. 6. Update the value of given integer variable s as the result of the mod of the given integer variable s and 3600. 7. Similarly, a variable of interger type minutes and initialise it's value as the result of the division of the given integer variable s and 60. 8. Update the value of given integer variable s as the result of the mod of the given integer variable s and 60. 9. After that, a variable of interger type variable seconds and initialise it as s. 10. Finally, print the integer variables days, hours, minutes and seconds.
Bir nümunə götürək s = 300 saniyə. İndi ilk növbədə gün sayını tapacağıq. Çünki bu, digərləri arasında maksimum saniyə olan (1 gün = 24 x 3600 s, 1 saat = 3600 s, 1 dəqiqə = 60 s) olan burada verilən vaxt vahididir. Beləliklə, s-ləri j24 x 3600-ə böldükdə 0-a bərabər bir nəticə əldə edəcəyik. Qalan 300-ə bərabərdir. Beləliklə, 0 günümüz var. İndi qalan saniyələrlə saat sayını tapırıq. Yenə də 300-ü 3600-ə bölürük. 0 nisbətini və qalan 300-ü alırıq. Beləliklə, qalan saniyələri 60-a bölərək bir neçə dəqiqə tapırıq. 5-ə, qalana 0-a bərabər oluruq. 300 s = 0 gün 0 saat 0 dəqiqə 0 saniyə əldə edin.
Kodu
Saniyələri günlərə çevirmək üçün C ++ Proqramı
#include <bits/stdc++.h> using namespace std; void convertSecond(int s){ int days = s/(24*3600); s = s%(24*3600); int hours = s/3600; s %= 3600; int minutes = s/60; s %= 60; int seconds = s; cout<<days<<"dd "<<hours<<"hh "<<minutes<<"mm "<<seconds<<"ss "; } int main(){ int s = 1543765; convertSecond(s); return 0; }
17dd 20hh 49mm 25ss
Saniyələri günlərə çevirmək üçün Java Proqramı
import java.io.*; class Seconds{ static void convertSecond(int s){ int days = s/(24*3600); s = s%(24*3600); int hours = s/3600; s %= 3600; int minutes = s/60; s %= 60; int seconds = s; System.out.println(days+"dd "+hours+"hh "+minutes+"mm "+seconds+"ss "); } public static void main (String[] args){ int s = 1543765; convertSecond(s); } }
17dd 20hh 49mm 25ss
Mürəkkəblik təhlili
Zaman mürəkkəbliyi
O (1) çünki daimi vaxtdan istifadə edirdik. Biz yalnız bəzi xüsusi formulları və bunları da tək bir girişdə.
Kosmik mürəkkəblik
O (1) çünki daimi yerdən istifadə etdik, ehtiyac olsa da müvəqqəti dəyişənlər sabit sayda idilər.