题目链接http://codeforces.com/problemset/problem/816/A
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.
The first and only line of input contains a single string in the format hh:mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59).
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.
05:39
11
13:31
0
23:59
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;
}