【26.34%】【codeforces 722A】Broken Clock

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59.

You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format.

For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39.

Input
The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively.

The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes.

Output
The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them.

Examples
input
24
17:30
output
17:30
input
12
17:30
output
07:30
input
24
99:99
output
09:09

【题解】

很容易想到如果超过了进制就把相应的位置的数字的第一位改为0;
但是有个坑点。
12进制的小时位
如果是20,不能改成00;
因为12进制是从1开始的。
方法是如果a%10==0,就把第一位改为1;
详细的看代码吧。

#include <cstdio>

int flag,a,b;
char s[50];

int main()
{
    //freopen("F:\\rush.txt", "r", stdin);
    scanf("%d", &flag);
    scanf("%s", s);
    int a = (s[0] - '0') * 10 + (s[1] - '0');
    int b = (s[3] - '0') * 10 + s[4] - '0';
    if (b > 59)
    {
        s[3] = '0';
    }
    if (flag == 12)
    {
        if (a > 12)
        {
            if ((a % 10) == 0)
            {
                s[0] = '1';
            }
            else
            {
                s[0] = '0';
            }
        }
        else
            if (a == 0)
            {
                s[0] = '1';
            }
    }
    else
    {
        if (a > 23)
        {
            s[0] = '0';
        }
    }
    printf("%s\n", s);
    return 0;
}
posted @ 2017-10-06 19:23  AWCXV  阅读(129)  评论(0编辑  收藏  举报