Raul Posted December 20, 2018 Report Posted December 20, 2018 (edited) Propun o noua problema, una care poate parea putin mai ciudata, dar este destul de interesanta. Dat orice interval orar (ora de pe ceas, efectiv), acesta se poate converti in cuvinte din limbaj natural, sub forma urmatoare: Pentru o valoare a minutului data, asta inseamna (notat cu m) cand m = 0, atunci se foloseste o' clock cand 1 <= m <= 30, atunci se foloseste past cand m > 30, atunci se foloseste to Se cere scrierea unei functii care returneaza timpul dat sub forma de text, in limbaj natural, respectand regulile de mai sus. Ca input, se dau doua valori intregi si naturale, notam h si m, unde h reprezinta ora, iar m reprezinta minutul. Constrangeri 1 <= h <= 12 0 <= m < 60 Exemple Pentru h = 5 si m = 47, rezulta "thirteen minutes to six" Pentru h = 3 si m = 00, rezulta "three o' clock" Pentru h = 7 si m = 15, rezulta "quarter past seven" Limbajul care va fi folosit este la alegere libera. Sunt acceptate toate solutiile, indiferent de complexitatea timp, dar trebuie incercat sa se rezolve in O(1). O solutie personala va fi pusa ulterior. Spor! Edited December 20, 2018 by Raul 2 Quote
aurelL Posted December 20, 2018 Report Posted December 20, 2018 (edited) Nu ştiu ce e ăia funcţie în programare, dar am încercat să fac ceva cu if, else if, else. Ce e O(1)? #include <iostream> using namespace std; int main() { int h, m; cin >> h >> m; if ( m == 0 ) { cout << h << " " << "o' clock"; } else if ( m >= 1 && m < 30 && m != 15 ) { cout << h << " " << "past" << " " << m; } else if ( m == 30 ) { cout << "half past" << " " << h; } else if ( m == 15 ) { cout << "quarter past" << " " << h; } else if ( m == 45 ) { cout << "quarter to" << " " << h+1; } else cout << 60-m << " " << "to" << " " << h+1; return 0; } Edited December 20, 2018 by aurelL 1 Quote
Raul Posted December 20, 2018 Author Report Posted December 20, 2018 (edited) 2 hours ago, aurelL said: Nu ştiu ce e ăia funcţie în programare, dar am încercat să fac ceva cu if, else if, else. Ce e O(1)? #include <iostream> using namespace std; int main() { int h, m; cin >> h >> m; if ( m == 0 ) { cout << h << " " << "o' clock"; } else if ( m >= 1 && m < 30 && m != 15 ) { cout << h << " " << "past" << " " << m; } else if ( m == 30 ) { cout << "half past" << " " << h; } else if ( m == 15 ) { cout << "quarter past" << " " << h; } else if ( m == 45 ) { cout << "quarter to" << " " << h+1; } else cout << 60-m << " " << "to" << " " << h+1; return 0; } Rezolvarea este partial corecta, deoarece trebuie tradus complet in text, la tine ramanand numerele netraduse. Pe langa asta, nu respecti, in totalitate, constrangerile. Ce sa-ti spun, iti recomand sa citesti si sa intelegeti tot ce zice aici (legat de prima dilema), si ce zice aici (legat de a doua dilema). Evident, astea sunt niste lucruri foarte basic, ar trebui sa le aprofundezi, daca chiar te intereseaza. Dupa aia, mai uita-te pe aici. Edited December 20, 2018 by Raul 1 Quote
TheOne01 Posted December 20, 2018 Report Posted December 20, 2018 (edited) Am si eu o intrebare: La 12:40 (de exemplu) ce ar trebui sa afiseze ? Edited December 20, 2018 by TheOne01 Quote
aurelL Posted December 20, 2018 Report Posted December 20, 2018 20 to 13. Adica ora unu fara 20 de minute. 1 <= h <= 12 1 Quote
Raul Posted December 20, 2018 Author Report Posted December 20, 2018 (edited) 29 minutes ago, TheOne01 said: Am si eu o intrebare: La 12:40 (de exemplu) ce ar trebui sa afiseze ? Twenty minutes to one. Ai grija la constrangeri. Edited December 20, 2018 by Raul Quote
TheOne01 Posted December 20, 2018 Report Posted December 20, 2018 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner( System.in ); int h = input.nextInt(); int m = input.nextInt(); whatTimeItIs(h, m); } public static void whatTimeItIs(int h, int m) { String[] hour = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "quarter", "sixteen", "seventeen", "eighteen", "nineteen"}; String result; boolean greater = false; if (m == 0) { result = hour[h-1] + " o' clock"; } else { if (m > 30) { m = 60 - m; greater = true; } if(m < 20) { result = hour[m - 1]; } else if (m == 30){ result = "half"; } else { result = m==20?"twenty":"twenty "+hour[m%10-1]; } if(m == 1){ result += " minute"; } else if(m != 15 && m != 30){ result += " minutes"; } if (!greater) { result += " past " + hour[h-1]; } else { result += " to "; if (h != 12) { result += hour[h]; } else { result += "one"; } } } System.out.println(result); } } 1 Quote
theandruala Posted December 21, 2018 Report Posted December 21, 2018 (edited) public class MuieGO3 { private static final String[] oreJmekere = { "", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" }; static final String[] minuteJmekere = { "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" }; public static void main(String[] args) { System.out.println(ceasCuCuc(2,01)); System.out.println(ceasCuCuc(2,10)); System.out.println(ceasCuCuc(2,15)); System.out.println(ceasCuCuc(2,28)); System.out.println(ceasCuCuc(2,30)); System.out.println(ceasCuCuc(2,40)); System.out.println(ceasCuCuc(2,45)); System.out.println(ceasCuCuc(2,47)); } static String ceasCuCuc(int ora, int minute) { String oraText = ""; if (minute == 0) { oraText = minuteJmekere[ora] + " o' clock"; } else if (minute == 15) { oraText = " quarter past" + minuteJmekere[ora]; } else if (minute < 30) { if (minute < 20) { oraText += minuteJmekere[minute]; } else { oraText += oreJmekere[minute / 10]; oraText += minuteJmekere[minute % 10]; } oraText += " minutes past" + minuteJmekere[ora]; } else if (minute == 30) { oraText += " half past" + minuteJmekere[ora]; } else if (minute == 45) { oraText += " quarter to" + minuteJmekere[ora + 1]; } else if (minute > 30) { if ((60 - minute) < 20) { oraText += minuteJmekere[60 - minute]; } else { oraText += oreJmekere[(60 - minute) / 10]; oraText += minuteJmekere[(60 - minute) % 10]; } oraText += " minutes to " + minuteJmekere[ora + 1]; } if(minute==1 || minute == 59){ oraText=oraText.replace("minutes", "minute"); } return oraText.trim(); } } Edited December 21, 2018 by theandruala 1 Quote
Raul Posted December 21, 2018 Author Report Posted December 21, 2018 (edited) 17 hours ago, TheOne01 said: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner( System.in ); int h = input.nextInt(); int m = input.nextInt(); whatTimeItIs(h, m); } public static void whatTimeItIs(int h, int m) { String[] hour = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "quarter", "sixteen", "seventeen", "eighteen", "nineteen"}; String result; boolean greater = false; if (m == 0) { result = hour[h-1] + " o' clock"; } else { if (m > 30) { m = 60 - m; greater = true; } if(m < 20) { result = hour[m - 1]; } else if (m == 30){ result = "half"; } else { result = m==20?"twenty":"twenty "+hour[m%10-1]; } if(m == 1){ result += " minute"; } else if(m != 15 && m != 30){ result += " minutes"; } if (!greater) { result += " past " + hour[h-1]; } else { result += " to "; if (h != 12) { result += hour[h]; } else { result += "one"; } } } System.out.println(result); } } Testat, este in regula, a trecut toate testcase-urile, kudos! 5 hours ago, theandruala said: public class MuieGO3 { private static final String[] oreJmekere = { "", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" }; static final String[] minuteJmekere = { "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" }; public static void main(String[] args) { System.out.println(ceasCuCuc(2,01)); System.out.println(ceasCuCuc(2,10)); System.out.println(ceasCuCuc(2,15)); System.out.println(ceasCuCuc(2,28)); System.out.println(ceasCuCuc(2,30)); System.out.println(ceasCuCuc(2,40)); System.out.println(ceasCuCuc(2,45)); System.out.println(ceasCuCuc(2,47)); } static String ceasCuCuc(int ora, int minute) { String oraText = ""; if (minute == 0) { oraText = minuteJmekere[ora] + " o' clock"; } else if (minute == 15) { oraText = " quarter past" + minuteJmekere[ora]; } else if (minute < 30) { if (minute < 20) { oraText += minuteJmekere[minute]; } else { oraText += oreJmekere[minute / 10]; oraText += minuteJmekere[minute % 10]; } oraText += " minutes past" + minuteJmekere[ora]; } else if (minute == 30) { oraText += " half past" + minuteJmekere[ora]; } else if (minute == 45) { oraText += " quarter to" + minuteJmekere[ora + 1]; } else if (minute > 30) { if ((60 - minute) < 20) { oraText += minuteJmekere[60 - minute]; } else { oraText += oreJmekere[(60 - minute) / 10]; oraText += minuteJmekere[(60 - minute) % 10]; } oraText += " minutes to " + minuteJmekere[ora + 1]; } if(minute==1 || minute == 59){ oraText=oraText.replace("minutes", "minute"); } return oraText.trim(); } } Testat, este in regula, a trecut toate testcase-urile, kudos! Solutia mea (Golang) este urmatoarea: var hes = map[int32]string{1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten", 11: "eleven", 12: "twelve", 13: "thirteen", 14: "fourteen", 15: "quarter", 16: "sixteen", 17: "seventeen", 18: "eighteen", 19: "nineteen", 20: "twenty", 21: "twenty one", 22: "twenty two", 23: "twenty three", 24: "twenty four", 25: "twenty five", 26: "twenty six", 27: "twenty seven", 28: "twenty eight", 29: "twenty nine", 30: "half"} func theClock(h int32, m int32) string { var out string if m == 0 { out = hes[h] + " o' clock" } else if m == 1 { out = hes[m] + " minute past " + hes[h] } else if m == 15 || m == 30 { out = hes[m] + " past " + hes[h] } else if m < 30 { out = hes[m] + " minutes past " + hes[h] } else if m == 45 { out = hes[60-m] + " to " + hes[h+1] } else { out = hes[60-m] + " minutes to " + hes[h+1] } return out } * Problema este adaptata de la ceva asemanator gasit pe HackerRank Edited December 21, 2018 by Raul 1 Quote
Oklah Posted January 17, 2019 Report Posted January 17, 2019 (edited) #include <iostream> using namespace std; int h,m; char O[11][100] = {"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; char M[19][150] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; char A[2][12] = {"quarter", "half"}; void trans() { if(m==60) cout<<M[h]<<" o'clock"; else if(m==0) cout<<M[h-1]<<" o'clock"; else if(m==1) cout<<M[m-1]<<" minute "<<"past "<<M[h-1]; if(m>1 && m<20 && m!= 15) cout<<M[m-1]<<" minutes "<<"past "<<M[h-1]; if(m==15) cout<<A[0]<<" minutes "<<"past "<<M[h-1]; else if(m==30) cout<<A[1]<<" minutes "<<"past "<<M[h-1]; else if(m==45) cout<<A[0]<<" minutes "<<"past "<<M[h-1]; if(m>20 && m<30) cout<<O[m/10%10-1]<<" "<<M[m%10-1]<<" minutes past "<<M[h-1]; else if(m>30 && m!=45 && m!=60){ if(60-m<20) cout<<M[60-m-1]<<" minutes to "<<M[h-1]; else cout<<O[(60-m)/10%10-1]<<" "<<M[(60-m)%10-1]<<" minutes to "<<M[h]; } } int main() { do{ cout<<"H = "; cin>>h; if(h<0 || h>12) cout<<"Ati introdus o ora gresita!"<<'\n';; }while(h<0 || h>12); do{ cout<<"M = "; cin>>m; if(m<0 || m>60) cout<<"Ati introdus minutele gresit!"<<'\n'; }while(m<0 || m>60); trans(); return 0; } Edited January 21, 2019 by Oklah Quote