The objective is to form the maximum possible time in the HH:MM:SS format using any six of nine given single digits (not necessarily distinct)
Given a set of nine single (not necessarily distinct) digits, say 0, 0, 1, 3, 4, 6, 7, 8, 9, it is possible to form many distinct times in a 12 hour time format HH:MM:SS, such as 10:36:40 or 01:39:46 by using each of the digits only once. The objective is to find the maximum possible valid time (00:00:01 to 12:00:00) that can be formed using some six of the nine digits exactly once. In this case, it is 10:49:38.
Input:
0,0,1,1,3,5,6,7,7
Output:
11:57:37
The maximum valid time in a 12 hour clock that can be formed using some six of the 9 digits precisely once is 11:57:37
Answers
Answered by
0
According to me it should be 10:59:48
Answered by
2
#include<stdio.h>
main()
{
int a[10],i,temp[10],HMS[6],x,z;
for(i=0;i<9;i++)
{
scanf("%1d,",&a[i]);
temp[i]=0;
}
for(i=0;i<=9;i++)
{
if(a[i]==0) temp[0]++;
else if(a[i]==1) temp[1]++;
else if(a[i]==2) temp[2]++;
else if(a[i]==3) temp[3]++;
else if(a[i]==4) temp[4]++;
else if(a[i]==5) temp[5]++;
else if(a[i]==6) temp[6]++;
else if(a[i]==7) temp[7]++;
else if(a[i]==8) temp[8]++;
else if(a[i]==9) temp[9]++;
}
if(temp[0]>0||temp[1]>0)
{
if(temp[1]==0)
{
HMS[0]=0;
temp[0]--;
for(x=9;x>0;x--)
{
if(temp[x]!=0)
{
HMS[1]=x;
temp[x]--;
break;
}
}
}
else
{
HMS[0]=1;
temp[1]--;
for(x=2;x>0;x--)
{
if(temp[x]!=0)
{
HMS[1]=x;
temp[x]--;
break;
}
}
}
if(HMS[0]==1&&HMS[1]!=2)
{
for(x=5;x>0;x--)
{
if(temp[x]!=0)
{
HMS[2]=x;
temp[x]--;
break;
}
}
for(x=9;x>0;x--)
{
if(temp[x]!=0)
{
HMS[3]=x;
temp[x]--;
break;
}
}
for(x=5;x>0;x--)
{
if(temp[x]!=0)
{
HMS[4]=x;
temp[x]--;
break;
}
}
for(x=9;x>0;x--)
{
if(temp[x]!=0)
{
HMS[5]=x;
temp[x]--;
break;
}
}
}
else
{
if(temp[0]==4)
HMS[2]=HMS[3]=HMS[4]=HMS[5]=0;
else goto z;
}
printf("%d%d:%d%d:%d%d",HMS[0],HMS[1],HMS[2],HMS[3],HMS[4],HMS[5]);
}
else //print impossible z: printf("\nimpossible\n");
}
main()
{
int a[10],i,temp[10],HMS[6],x,z;
for(i=0;i<9;i++)
{
scanf("%1d,",&a[i]);
temp[i]=0;
}
for(i=0;i<=9;i++)
{
if(a[i]==0) temp[0]++;
else if(a[i]==1) temp[1]++;
else if(a[i]==2) temp[2]++;
else if(a[i]==3) temp[3]++;
else if(a[i]==4) temp[4]++;
else if(a[i]==5) temp[5]++;
else if(a[i]==6) temp[6]++;
else if(a[i]==7) temp[7]++;
else if(a[i]==8) temp[8]++;
else if(a[i]==9) temp[9]++;
}
if(temp[0]>0||temp[1]>0)
{
if(temp[1]==0)
{
HMS[0]=0;
temp[0]--;
for(x=9;x>0;x--)
{
if(temp[x]!=0)
{
HMS[1]=x;
temp[x]--;
break;
}
}
}
else
{
HMS[0]=1;
temp[1]--;
for(x=2;x>0;x--)
{
if(temp[x]!=0)
{
HMS[1]=x;
temp[x]--;
break;
}
}
}
if(HMS[0]==1&&HMS[1]!=2)
{
for(x=5;x>0;x--)
{
if(temp[x]!=0)
{
HMS[2]=x;
temp[x]--;
break;
}
}
for(x=9;x>0;x--)
{
if(temp[x]!=0)
{
HMS[3]=x;
temp[x]--;
break;
}
}
for(x=5;x>0;x--)
{
if(temp[x]!=0)
{
HMS[4]=x;
temp[x]--;
break;
}
}
for(x=9;x>0;x--)
{
if(temp[x]!=0)
{
HMS[5]=x;
temp[x]--;
break;
}
}
}
else
{
if(temp[0]==4)
HMS[2]=HMS[3]=HMS[4]=HMS[5]=0;
else goto z;
}
printf("%d%d:%d%d:%d%d",HMS[0],HMS[1],HMS[2],HMS[3],HMS[4],HMS[5]);
}
else //print impossible z: printf("\nimpossible\n");
}
Similar questions