Advertisement
Aniket_Goku

max_time

Jan 30th, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. /*
  2. 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.
  3. example
  4. (1) set={0,1,3,4,2,1,5,8,0}
  5.     ans = 11:58:43
  6. (2) set={0,9,6,9,7,8,9,6,3}
  7.     ans = Impossible opretion!
  8. */
  9. //9 digit to time
  10.  
  11. #include<iostream.h>
  12. #include<stdio.h>
  13. #include<conio.h>
  14. int sec,min,hh,ten=0,top=9;
  15. class time
  16. {
  17.     int i;
  18.     public:
  19.     int getdata()
  20.     {
  21.  
  22.             cout<<"\nEnter the value: ";
  23.             cin>>i;
  24.             return i;
  25.  
  26.     }
  27.     int getans(int num[],int no)
  28.     {
  29.         int i,k,com,max;
  30.         max=com=0;
  31.         for(i=0;i<top;i++)
  32.         {
  33.             for(k=0;k<top;k++)
  34.             {
  35.                 if(i!=k)
  36.                 {
  37.                     com=(num[i]*10)+num[k];
  38.                     //cout<<" "<<com;
  39.                 }
  40.                 if(com>max && com<no)
  41.                 {
  42.                     max=com;
  43.                 }
  44.             }
  45.         }
  46.         return max;
  47.     }
  48.     void transfer(int used[],int no)
  49.     {
  50.  
  51.  
  52.             used[0]=no/10;
  53.             used[1]=(no%10);
  54.  
  55.     }
  56.     void cuts(int num[],int used[])
  57.     {
  58.         int j=0,k;
  59.  
  60.         for(i=0;i<top;i++)
  61.         {
  62.             if(num[i]==used[j]  && j<2)
  63.             {
  64.                 if(i!=top-1)
  65.                 {
  66.                     for(int k=i;k<top-1;k++)
  67.                     {
  68.                         num[k]=num[k+1];
  69.                     }
  70.                 }
  71.                   top--;
  72.                   j++;
  73.                   i--;
  74.             }
  75.         }
  76.  
  77.     }
  78.  
  79.  
  80.  
  81. };
  82.  
  83. int main()
  84. {
  85.     time t1;
  86.     int num[9],i,k,used[3];
  87.     clrscr();
  88.     //insertion
  89.     cout<<"\n Enter 9 digits: ";
  90.     for(i=0;i<top;i++)
  91.     {
  92.         num[i]=t1.getdata();
  93.     }
  94.     // displayed
  95.     cout<<"\n User's content\n (";
  96.     for(i=0;i<top;i++)
  97.     {
  98.         cout<<" "<<num[i];
  99.  
  100.     }
  101.      cout<<")\n";
  102.     // hh
  103.     hh = t1.getans(num,12);
  104.  
  105.     t1.transfer(used,hh);
  106.     // cut digits from num array of hour
  107.     t1.cuts(num,used);
  108.  
  109.     //min
  110.     used[0]=0;
  111.     used[1]=0;
  112.     //get minute
  113.     min=t1.getans(num,60);
  114.     // cut digits from num array of minute
  115.     t1.transfer(used,min);
  116.  
  117.     t1.cuts(num,used);
  118.  
  119.     //sec
  120.     used[0]=0;
  121.     used[1]=0;
  122.     //get ans of second
  123.     sec=t1.getans(num,60);
  124.     // cut digits from num array of second
  125.     t1.transfer(used,sec);
  126.  
  127.     // if h,r,m ==0 the operation is impossible
  128.     if(hh==0 || min==0 || sec==0)
  129.     {
  130.         cout<<"\n operation is impossible!";
  131.     }
  132.     else
  133.     {
  134.         cout<<"\n hh: mm: ss  ";
  135.         cout<<"\n "<<hh<<":"<<min<<":"<<sec;
  136.     }
  137.     getch();
  138.     return 0;
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement