We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Warmup
- Time Conversion
- Discussions
Time Conversion
Time Conversion
Sort by
recency
|
4967 Discussions
|
Please Login in order to post a comment
Easy python solution
include
include
include
include
char a[1000][10]; int index = 0;
int check(char c[]){ for(int i = 0; i < strlen(c); i++){ if(c[i] == 'P'){ return 1; } } return 0; }
char *coppy(char c[]){ char tmp = (char)malloc(sizeof(strlen(c) + 1)); int j = 0; for(int i = 0; i < strlen(c); i++){ if(c[i] == 'P' || c[i] == 'A'){ break; } else { tmp[j] = c[i]; j++; } } tmp[j] = '\0'; return tmp; }
int main(){ char c[1001]; fgets(c, 1001, stdin); c[strlen(c) - 1] = '\0'; char *token = strtok(c, ":"); //tach tu while(token != NULL){ strcpy(a[index], token); index++; token = strtok(NULL, ":"); } int hour = atoi(a[0]); if(check(a[index - 1]) == 1){ if(hour == 12){ printf("%d:%s:%s", hour, a[1], coppy(a[index - 1])); } else { hour += 12; printf("%d:%s:%s", hour, a[1], coppy(a[index - 1])); } } else { if(hour == 12){ printf("00:%s:%s", a[1], coppy(a[index - 1])); } else { printf("%s:%s:%s", a[0], a[1], coppy(a[index - 1])); } } return 0; }
Python solution
s=input() l=int(s[:2]) ampm=s[8:] if ampm=='PM' and l<12: l+=12 elif ampm=='AM' and l==12: l=0 time='{:02d}'.format(l) ans=time+s[2:8] print(ans)
My C# Solution