Sherlock and The Beast

  • + 0 comments

    This is my solution. Count 5 (the number of '5' digits) starts completely divisible by 3. Count 3 (the number of '3' digits) is basically the remainder, after substracting count 5 from n. We need to remove multiples of 3 from count5 and add them to count3 until count3 will become divisible by 5. It won't take long. Think about it, any number divided by 5 will have the following remainders 0, 1, 2, 3, 4. If it's 0 you're already there. If it's 1, you need to add 3 three times and consider modulo 5 group with addition: 1 + 3 = 4; 4 + 3 = 2; 2 + 3 = 0; If it's 2, you only need one addition to 3: 2 + 3 = 0 For 3: 3 + 3 = 1; 1 + 3 = 4; 4 + 3 = 2; 2 + 3 = 1 For 4: 4 + 3 = 2; 2 + 3 = 0;

    void decentNumber(int n) { int count5 = (n / 3) * 3; int count3 = n - count5;

    while(count3 % 5)
    {
        count3 += 3;
    }
    
    count5 = n - count3;
    if(count5 < 0)
    {
        cout << "-1" << endl;
        return;
    }
    for(int i = 0; i < count5; i++)
    {
        cout << "5";
    }
    for(int i = 0; i < count3; i++)
    {
        cout << "3";
    }
    
    cout << endl;
    

    }