publicclassColorHSB{privatefinalint h, s, b;// Creates a color with hue h, saturation s, and brightness b.publicColorHSB(int h,int s,int b){if(h <0|| h >359|| s <0|| s >100|| b <0|| b >100){thrownewIllegalArgumentException();}this.h = h;this.s = s;this.b = b;}// Returns a string representation of this color, using the format (h, s, b).publicStringtoString(){return"("+ h +", "+ s +", "+ b +")";}// Is this color a shade of gray?publicbooleanisGrayscale(){return s ==0|| b ==0;}// Returns the squared distance between the two colors.publicintdistanceSquaredTo(ColorHSB that){if(that ==null){thrownewIllegalArgumentException();}int res =0;int h1 =this.h, h2 = that.h;int s1 =this.s, s2 = that.s;int b1 =this.b, b2 = that.b;
res +=Math.min((h1 - h2)*(h1 - h2),(360-Math.abs(h1 - h2))*(360-Math.abs(h1 - h2)));
res +=(s1 - s2)*(s1 - s2);
res +=(b1 - b2)*(b1 - b2);return res;}// Sample client (see below).publicstaticvoidmain(String[] args){int h =Integer.parseInt(args[0]);int s =Integer.parseInt(args[1]);int b =Integer.parseInt(args[2]);ColorHSB color =newColorHSB(h, s, b);String name =null;ColorHSB ans =null;int min =Integer.MAX_VALUE;while(!StdIn.isEmpty()){String tmpName =StdIn.readString();
h =Integer.parseInt(StdIn.readString());
s =Integer.parseInt(StdIn.readString());
b =Integer.parseInt(StdIn.readString());ColorHSB tmpColor =newColorHSB(h, s, b);int dist = color.distanceSquaredTo(tmpColor);if(min > dist){
min = dist;
name = tmpName;
ans = tmpColor;}}StdOut.println(name +" "+ ans);}}
publicclassClock{privateint h;privateint m;// Creates a clock whose initial time is h hours and m minutes.publicClock(int h,int m){if(h <0|| h >23|| m <0|| m >59){thrownewIllegalArgumentException();}this.h = h;this.m = m;}// Creates a clock whose initial time is specified as a string, using the format HH:MM.publicClock(String s){if(s.length()!=5|| s.charAt(2)!=':'){thrownewIllegalArgumentException();}// 输入的参数有非数字for(int i =0; i < s.length(); i++){if(i !=2&&!Character.isDigit(s.charAt(i))){thrownewIllegalArgumentException();}}this.h =Integer.parseInt(s.substring(0,2));this.m =Integer.parseInt(s.substring(3,5));if(h <0|| h >23|| m <0|| m >59){thrownewIllegalArgumentException();}}// Returns a string representation of this clock, using the format HH:MM.publicStringtoString(){returnString.format("%02d:%02d", h, m);}// Is the time on this clock earlier than the time on that one?publicbooleanisEarlierThan(Clock that){if(this.h == that.h){returnthis.m < that.m;}returnthis.h < that.h;}// Adds 1 minute to the time on this clock.publicvoidtic(){
m +=1;
h += m /60;
m %=60;
h %=24;}// Adds Δ minutes to the time on this clock.publicvoidtoc(int delta){if(delta <0){thrownewIllegalArgumentException();}
m += delta;
h += m /60;
m %=60;
h %=24;}// Test client (see below).publicstaticvoidmain(String[] args){Clock clock =newClock(args[0]);StdOut.println(clock.toString());
clock.tic();StdOut.println(clock.toString());
clock.toc(60);StdOut.println(clock.toString());}}