The maximum integer that can be divisible by 15

Source: Internet
Author: User

The maximum integer that can be divisible by 15
2010/05/15 pm
Question

Given a string that only contains numbers [0 .. 9], use some characters in the string to construct the maximum integer that can be divisible by 15. Note that each character in a string can only be used once.
Input: The program reads data from the standard input. each row of data consists of a string of numbers. The length ranges from 1 to 1000.
Output: outputs a result for each input row. Each result occupies one row. If an integer that can be divisible by 15 cannot be constructed, output impossible.

Solutions

Since it can be divided by 15, first ensure that it can be divided by 5, then the last one is either 0 or 5, and then it must be divided by 3, the sum of numbers must be a multiple of 3. This question is based on these two points.

Step 1: add the number given by the question, and judge each step based on the remainder of 3. When the remainder is 0, the number can be sorted in ascending order and the last digit is 0 or 5. If this is not met, the output cannot be found.

When the remainder is 1, it is noted that there may be one of the numbers 1, 4, and 7, or the sum of two numbers except 3 and 2. In this case, we need to judge them separately, remove it and determine whether the last digit meets the requirements.

For the case where the remainder is 2, it is similar to the previous case that there are 2, 5, 8, or the sum of two numbers except 3 and 1.

 

#include<iostream>#include<string>using namespace std;int main(){char a[1001];int b[10];int i;int sum;int upseter;while(cin>>a){   upseter=1;   for(i=0;i<10;i++)    b[i]=0;     sum=0;   int length=strlen(a);   for(i=0;i<length;i++)   {    b[a[i]-'0']++;    sum+=a[i]-'0';   }   if(sum==0)   { upseter=0;    cout<<"0"<<endl;   }   else   {    if(sum%3==0)    {a:     int upset=0;     for(i=1;i<10;i++)      upset+=b[i];     if(upset==0)      goto abc;     if(b[0]>0)     {      for(i=9;i>=0;i--)      {       for(int j=0;j<b[i];j++)        cout<<i;      }      cout<<endl;      upseter=0;      goto abc;     }//b[0]>0     else     {      if(b[5]>0)      {       b[5]=b[5]-1;       for(i=9;i>=0;i--)       {        for(int j=0;j<b[i];j++)         cout<<i;       }       cout<<"5"<<endl;       upseter=0;       goto abc;      }      else       goto abc;     }//b[5]>0 or b[0]+ b[5]=0    } //%3==0    if(sum%3==1)    {     int flag1=0;     for(i=1;i<=7;i+=3)     {      if(b[i]>0)      {        b[i]--;       flag1=1;         break;      }     }//find the %3==1 number     if(flag1==1)     {        goto a;      }//the number has been found     else     {      int upset1=0;      if(b[2]+b[5]+b[8]>=2)      {       for(i=2;i<=8;i+=3)       {        if(b[i]>0)        {         b[i]--;         upset1++;        }        if(upset1==2)         break;       }        goto a;         }//there are two %3==2 numbers      else       goto abc;                    }         }    if(sum%3==2)    {     int flag2=0;     for(i=2;i<=8;i+=3)     {      if(b[i]>0)      {        b[i]--;       flag2=1;         break;      }     }//find the %3==2 number     if(flag2==1)     {        goto a;      }//the number has been found     else     {      int upset2=0;      if(b[1]+b[4]+b[7]>=2)      {       for(i=1;i<=7;i+=3)       {        if(b[i]>0)        {         b[i]--;         upset2++;        }        if(upset2==2)         break;       }        goto a;         }//there are two %3==1 numbers      else       goto abc;                    }        }   }abc: if(upseter==1)   cout<<"impossible"<<endl;  }   return 0;}

PS: Some people have reported that this Code cannot pass all the test cases in the network classroom. It seems that Mr. Lin Yonggang has added the test cases and does not know what BT use cases are.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.