TCHS-6-250

Problem Statement

     A digital clock displays time in the format "DD:DD:DD" (quotes for clarity only), where each D is a single digit, and each pair of contiguous digits represents either the hour, minute, or second (each of these three units will occur exactly once). Unfortunately, we do not know the ordering of the time units, so need to figure out the number of valid ways to interpret the displayed time.

The hour must be between 01 and 12, inclusive, the minute must be between 00 and 59, inclusive, and the second must be between 00 and 59, inclusive.
For example, "21:23:01" can be intepreted in two ways: "minute:second:hour" or "second:minute:hour".

Definition

    
Class: DigitalDisplay
Method: waysToInterpret
Parameters: String
Returns: int
Method signature: int waysToInterpret(String display)
(be sure your method is public)
    
 

Constraints

- display will contain exactly 8 characters.
- display will be formatted as "DD:DD:DD" (quotes for clarity only), where each D is a digit ('0'-'9').

Examples

0)  
    
"21:23:01"
 
Returns: 2
 
The example from the problem statement.
1)  
    
"00:00:00"
 
Returns: 0
 
There are no valid interpretations here because the hour must be at least 01.
2)  
    
"01:02:03"
 
Returns: 6
 
 
3)  
    
"59:59:01"
 
Returns: 2
 
 
4)  
    
"01:01:59"
 
Returns: 4
 
The four ways to interpret this are:
"hour:minute:second",
"hour:second:minute",
"minute:hour:second",
"second:hour:minute".

public class DigitalDisplay {

	public static int waysToInterpret(String s) {
		int a = Integer.parseInt(s.substring(0, 2));
		int b = Integer.parseInt(s.substring(3, 5));
		int c = Integer.parseInt(s.substring(6, 8));
		int count = 0;
		if (a >= 1 && a <= 12 && b >=0 && b <= 59 && c >=0 && c <= 59)
			count += 2;
		if (b >= 1 && b <= 12 && a >=0 && a <= 59 && c >=0 && c <= 59)
			count += 2;
		if (c >= 1 && c <= 12 && b >=0 && b <= 59 && a >=0 && a <= 59)
			count += 2;
		return count;
	}

}

 

你可能感兴趣的:(C++,c,C#)