Karen and Morning

本文介绍 CodeForces 816A 题目解法,该题要求计算从当前时间到达下一个回文时间所需的最短分钟数。通过循环递增时间并检查是否为回文来解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接http://codeforces.com/problemset/problem/816/A

A. Karen and Morning
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Karen is getting ready for a new school day!

It is currently hh:mm, given in a 24-hour format. As you know, Karen loves palindromes, and she believes that it is good luck to wake up when the time is a palindrome.

What is the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome?

Remember that a palindrome is a string that reads the same forwards and backwards. For instance, 05:39 is not a palindrome, because05:39 backwards is 93:50. On the other hand, 05:50 is a palindrome, because 05:50 backwards is 05:50.

Input

The first and only line of input contains a single string in the format hh:mm (00 ≤  hh  ≤ 2300 ≤  mm  ≤ 59).

Output

Output a single integer on a line by itself, the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome.

Examples
input
05:39
output
11
input
13:31
output
0
input
23:59
output
1
题目大意:

输入一个字符串,表示一个时间。计算经过多长分钟后  到达的时间是  回文串  。并输出分钟数。

解题思路:

定一个整型变量sum,用于存储经过的分钟数;用一个while循环,首先判断是否为回文串。如果是的话就直接输出sum的值,即0。不是的话让分钟数加1(这里注意涉及相应的时间进位),sum的值也加1,然后判断得到的时间是否为回文串,如果是的话,输出sum的值,跳出循环;不是的话继续进行while循环,直到得到的时间为回文串。

打吗:

#include<iostream>
using namespace std;
int qq(string s)                //判断是否为回文串的函数
{
    if(s[0]==s[4]&&s[1]==s[3])
        return 1;
    else
        return 0;
}
int main()
{
    string s;
    int sum;
    while(cin>>s)
    {
        sum=0;
        if(qq(s)==1)            //如果给定的时间直接就是回文串,那么输出sum,继续进行下一次输入
        {
            cout<<sum<<endl;
            continue;
        }
        while(1)
        {
            sum++;
            s[4]=s[4]+1;        //分钟数加1
            if(s[4]-'0'==10)    //需要进位
            {
                s[4]='0';
                s[3]=s[3]+1;
                if(s[3]=='6')   //进位
                {
                    s[3]='0';
                    s[1]=s[1]+1;
                    if(s[1]=='4'&&s[0]=='2')  //如果是24:00,即00:00(也是回文串),所以输出sum值跳出循环
                    {
                        cout<<sum<<endl;
                        break;
                    }
                    else
                    {
                        if(s[1]-'0'==10)    //进位
                        {
                            s[1]='0';
                            s[0]=s[0]+1;
                            if(s[0]=='2'&&s[1]=='4')    //如果是24:00,即00:00(也是回文串),所以输出sum值跳出循环
                            {
                                cout<<sum<<endl;
                                break;
                            }
                        }
                    }
                }
            }
            if(qq(s)==1)                 //如果加1分钟后 得到的 时间为回文串,那么输出sum值,跳出循环
            {
                cout<<sum<<endl;
                break;
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值