ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1009. K-based Numbers

Compilation error. What's the problem?
Posted by Afli 16 Apr 2012 21:02
#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

int main(int Argc, char* pszArgs[])
{
    int K, N;
    cin >> N;
    if (N < 2) exit(0);
    cin >> K;
    if ((K < 2) || (K > 10)) exit(0);
    if ((K + N) > 18) exit(0);

    long nBegin = 1,
         nEnd = 10;
    for(int i = 1; i < N; i++)
    {
        nBegin *= 10;
        nEnd *= 10;
    }

    long nAmount = nEnd - nBegin,
         nDivisor1,
         nDivisor2,
         k = 0;
    bool tester;
    long a[nAmount];

    for(int i = nBegin; i < nEnd; i++)
    {
        tester = true;
        nDivisor1 = nBegin;

        for(int j = 0; j < N; j++)
        {
            if(((i / nDivisor1) % 10) >= K)
            {
            nAmount--;
            tester = false;
            break;
            }
            nDivisor1 /= 10;
        }
        if(tester == true)
        {
            a[k] = i;
            k++;
        }
    }

    long answer = nAmount;
    for(int i = 0; i <= nAmount; i++)
    {
        nDivisor1 = nBegin;
        nDivisor2 = nBegin / 10;
        for(int j = 0; j < N; j++)
        {
            if( (((a[i] / nDivisor1) % 10) == 0) && (((a[i] / nDivisor2) % 10) == 0) )
            {
            answer--;
            break;
            }
            {
            nDivisor1 /= 10;
            nDivisor2 /= 10;
            }
            if(nDivisor2 == 0) break;
        }
    }
    cout << answer << endl;

    system("pause");
    return 0;
}
Re: Compilation error. What's the problem?
Posted by vlyubin 18 Apr 2012 08:39
You can click on Compilation error in your submissions list and get a detailed feedback.
My suggestions would be:
1)system("pause");
I have no I idea what this is, but if this pauses the system (Captain obvious says hello :)  ) then this is a forbidden function. Probably you used this for debugging purposes and forgot to remove before submission.
2)Change "int main(int Argc, char* pszArgs[])" to "int main()", though this is 1% likely.
3)I don't know whether exit's are allowed, try to avoid them if possible.

Good luck ! Most likely it's the reason 1.
Re: Compilation error. What's the problem?
Posted by Afli 18 Apr 2012 16:45
Thank u:)
I removed "system("pause");", and corrected int main, but the problem still exists.
Here are errors, that the compiler writes:
24ea482c-ee2f-4ec9-b4f0-97622aa086ca
24ea482c-ee2f-4ec9-b4f0-97622aa086ca(34) : error C2057: expected constant expression
24ea482c-ee2f-4ec9-b4f0-97622aa086ca(34) : error C2466: cannot allocate an array of constant size 0
24ea482c-ee2f-4ec9-b4f0-97622aa086ca(34) : error C2133: 'a' : unknown size

Do u have any ideas?
Re: Compilation error. What's the problem?
Posted by Afli 18 Apr 2012 18:37
I have corrected my code like this:

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

int main()
{
    int K, N;
    cin >> N;
    if (N < 2) exit(0);
    cin >> K;
    if ((K < 2) || (K > 10)) exit(0);
    if ((K + N) > 19) exit(0);

    long nBegin = 1,
         nEnd = 10;
    for(int i = 1; i < N; i++)
    {
        nBegin *= 10;
        nEnd *= 10;
    }

    long nAmount = nEnd - nBegin,
         nDivisor;
    long a[N];

    for(long i = nBegin; i < nEnd; i++)
    {
       nDivisor = nBegin;

        for(int j = 0; j < N; j++)
        {
            a[j] = (i / nDivisor) % 10;
            nDivisor /= 10;
            if (nDivisor == 0) nDivisor = 1;
        }
        for(int j = 0; j < N; j++)
        {
            if((a[j] >= K) || ((a[j] == 0) && (a[j+1] == 0)))
            {
            nAmount--;
            break;
            }
        }
    }

    cout << nAmount;

    return 0;
}

I still have this errors:
24ea482c-ee2f-4ec9-b4f0-97622aa086ca
24ea482c-ee2f-4ec9-b4f0-97622aa086ca(34) : error C2057: expected constant expression
24ea482c-ee2f-4ec9-b4f0-97622aa086ca(34) : error C2466: cannot allocate an array of constant size 0
24ea482c-ee2f-4ec9-b4f0-97622aa086ca(34) : error C2133: 'a' : unknown size

But if I change the size of array to a constant(Ex: a[16]), I have "Time limit exeeded". What should I do?


Edited by author 18.04.2012 18:38
Re: Compilation error. What's the problem?
Posted by vlyubin 19 Apr 2012 04:47
You cannot statically allocate an array of unknown size (at compilation stage i.e. size is a variable). Though you can do it dynamically, like "int * a = new int[N]".
Now, "Time limit" means that your program actually runs, but it's too slow. Therefore the problem lies in the algorithm you use. Try to apply dynamic programming. The solution is really easy, my main() method has exactly 10 lines (and it's the only method).
Try to think about the number of sequences of certain length that end with zero and the ones that don't. Try to combine these numbers...

Good luck !