Likou 1736. The latest time obtained by replacing hidden numbers-C language implementation-simple questions

topic

Portal

Give you a string of time, the format is hh:mm (hours: minutes), in which certain digits are hidden (indicated by ?).
The valid time is all times between 00:00 and 23:59, including 00:00 and 23:59.
Replace the hidden number in time and return the latest valid time you can get.

Example 1:

Input: time = "2?:?0"
Output: "23:50"
Explanation: The latest hour starting with the number '2' is 23, and the latest minute ending with '0' is 50.

Example 2:

Input: time = "0?:3?"
Output: "09:39"

Example 3:

Input: time = "1?:22"
Output: "19:22"

prompt:

time 的格式为 hh:mm
题目数据保证你可以由输入的字符串生成有效的时间

answer

template

char * maximumTime(char * time){
    
    

}

analysis

This question does not have any high requirements, mainly because certain conditions are required for some situations:
according to different digits, the above is'? 'We can classify: the
first place: the
first place must be 2, but in fact, there is a requirement. If the second place is a number more than 3, then naturally it cannot be filled with 2. At this time, it is used 1 to fill, except for the case where the second digit is a number, the second digit may also be? At this time, 2 can also be used for the first position. So there are the following judgments:

if(i==0)
            {
    
    
            if(time[1]<'4'||time[1]=='?')time[i]='2';
            else time[i]='1';
            }

Second place: At this time, we need to consider the number of the first place to determine the upper limit of the second place. If the first place is 2 then only 3 can be filled, but if it is not 2, it can be filled with 9;

  if(i==1)
            {
    
    
                if(time[0]=='2')time[i]='3';
                else time[i]='9';
            }

The third place is ":" without a situation analysis. The
fourth place, the fifth place:
neither of these two is very particular, the fourth place is 5, and the fifth place is 9, so you can get the following code:

  	if(i==3)time[i]='5';
	if(i==4)time[i]='9'; 

Complete code

char * maximumTime(char * time){
    
    
    for(int i=0;i<5;i++)
    {
    
    
        if(time[i]=='?')
        {
    
    
            if(i==2)continue;
            if(i==0)
            {
    
    
            if(time[1]<'4'||time[1]=='?')time[i]='2';
            else time[i]='1';
            }
            if(i==1)
            {
    
    
                if(time[0]=='2')time[i]='3';
                else time[i]='9';
            }
            if(i==3)time[i]='5';
            if(i==4)time[i]='9'; 
        }
    }
    return time;
}

run

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44922487/article/details/113831636