Suiiinaldo's blog

By Suiiinaldo, history, 2 years ago, In English

Question Link:

1692D - The Clock

Here is my code:

#include<bits/stdc++.h>
#define upper(s) transform(s.begin(),s.end(),s.begin(),::toupper)
#define lower(s) transform(s.begin(),s.end(),s.begin(),::tolower)
#define all(s) s.begin(),s.end()
#define rep(m) for(int i=0;i<n;i++)
#define showvec(v) for(int i=0;i<v.size();i++) cout<<v[i]<<" ";cout<<endl
#define showmap(m) for(auto i=m.begin();i!=m.end();++i) cout<<i->first<<" "<<i->second<<endl;
#define ll long long
using namespace std;
bool CheckPalindrome(string a,string b)
{
    reverse(all(a));
    if(a==b)
    return true;
    else
    return false;
}
void solve()
{
    string FullTime;
    cin>>FullTime;
    ll AddedTime;
    cin>>AddedTime;
    int InitialHour=stoi(FullTime.substr(0,2));
    int InitialMinute=stoi(FullTime.substr(3,4));
    string HoursFirstCheck=to_string(InitialHour);
    string MinutesFirstCheck=to_string(InitialMinute);
    int UpdatedHour=InitialHour;int UpdatedMinute=InitialMinute;int counter=0;
    if(CheckPalindrome(HoursFirstCheck,MinutesFirstCheck))
    counter++;
    do
    {
        UpdatedHour+=AddedTime/60;
        if(UpdatedHour>23)
            UpdatedHour=UpdatedHour-24;
        UpdatedMinute+=AddedTime%60;
        if(UpdatedMinute>60)
        {
            UpdatedMinute-=60;
            UpdatedHour+=1;
        }
        string MinutesFormat=to_string(UpdatedMinute);
        string HoursFormat=to_string(UpdatedHour);
        if(UpdatedHour<10)
        {
            HoursFormat="0"+HoursFormat;
        }
        if(UpdatedMinute<10)
        {
            MinutesFormat="0"+MinutesFormat;
        }
        cout<<HoursFormat<<"   "<<MinutesFormat<<endl;
        if(CheckPalindrome(HoursFormat,MinutesFormat))
        counter++;
    }while(UpdatedHour!=InitialHour and UpdatedMinute!=InitialMinute);
    cout<<counter<<endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    int t;
    cin>>t;
    while(t-->0)
    {
        solve();
    }
    return 0;
}
  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
  Vote: I like it +3 Vote: I do not like it

You are updating minutes afterward and if it is >60 you are increasing the hour count but suppose that if the initial time is 23:50 and u have to update it by 40 mins ur code will make the updated time as 24:30 which can never happen. So, after increasing the hour check once again whether it is greater than 23. Also in the minute condition, it should be >59 instead of >60.

»
2 years ago, # |
  Vote: I like it +1 Vote: I do not like it

what are these variable names

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Maybe he updated the names before asking for help to make the code more readable