Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- given a set of nine single digits(not necesserily distinct) say 0,0,1,3,4,6,7,8,9 . it is possible to form many distinct times in 12 hour time format HH:MM:SS , such as 10:36:40 or 01:39:46 by use of digits only once. the objective is to find maximum positive valid time (00:00:01 to 12:00:00) . that can be formed using some six of nine digits exectly once. in this case it is 10:49:38.
- example
- (1) set={0,1,3,4,2,1,5,8,0}
- ans = 11:58:43
- (2) set={0,9,6,9,7,8,9,6,3}
- ans = Impossible opretion!
- */
- //9 digit to time
- #include<iostream.h>
- #include<stdio.h>
- #include<conio.h>
- int sec,min,hh,ten=0,top=9;
- class time
- {
- int i;
- public:
- int getdata()
- {
- cout<<"\nEnter the value: ";
- cin>>i;
- return i;
- }
- int getans(int num[],int no)
- {
- int i,k,com,max;
- max=com=0;
- for(i=0;i<top;i++)
- {
- for(k=0;k<top;k++)
- {
- if(i!=k)
- {
- com=(num[i]*10)+num[k];
- //cout<<" "<<com;
- }
- if(com>max && com<no)
- {
- max=com;
- }
- }
- }
- return max;
- }
- void transfer(int used[],int no)
- {
- used[0]=no/10;
- used[1]=(no%10);
- }
- void cuts(int num[],int used[])
- {
- int j=0,k;
- for(i=0;i<top;i++)
- {
- if(num[i]==used[j] && j<2)
- {
- if(i!=top-1)
- {
- for(int k=i;k<top-1;k++)
- {
- num[k]=num[k+1];
- }
- }
- top--;
- j++;
- i--;
- }
- }
- }
- };
- int main()
- {
- time t1;
- int num[9],i,k,used[3];
- clrscr();
- //insertion
- cout<<"\n Enter 9 digits: ";
- for(i=0;i<top;i++)
- {
- num[i]=t1.getdata();
- }
- // displayed
- cout<<"\n User's content\n (";
- for(i=0;i<top;i++)
- {
- cout<<" "<<num[i];
- }
- cout<<")\n";
- // hh
- hh = t1.getans(num,12);
- t1.transfer(used,hh);
- // cut digits from num array of hour
- t1.cuts(num,used);
- //min
- used[0]=0;
- used[1]=0;
- //get minute
- min=t1.getans(num,60);
- // cut digits from num array of minute
- t1.transfer(used,min);
- t1.cuts(num,used);
- //sec
- used[0]=0;
- used[1]=0;
- //get ans of second
- sec=t1.getans(num,60);
- // cut digits from num array of second
- t1.transfer(used,sec);
- // if h,r,m ==0 the operation is impossible
- if(hh==0 || min==0 || sec==0)
- {
- cout<<"\n operation is impossible!";
- }
- else
- {
- cout<<"\n hh: mm: ss ";
- cout<<"\n "<<hh<<":"<<min<<":"<<sec;
- }
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement