Digit Generator generate meta (P52)/UVA-1583

Source: Internet
Author: User

1. If each test data, starting from its own to find its generator, it can also be done ....

But.... Will time out: Every one to check ... It's obviously too slow.

2. However, given the range of data, the maximum number of lookup generators is 10000, which can be used to make a minimum of 10000 of the number of generated meta-tables.

Check the table when you test the data.


The first code is TE's: (Very long with wood there ...) )

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
using namespace std;

int main ()
{
    int T;
    scanf ("%d", &t);

    int sheng[105];//
    while (t--)
    {
        memset (sheng,0,sizeof (Sheng));
        int n;
        scanf ("%d", &n);

        int i = 0;
        int m;
        for (M = n;m > 0;m--)
        {
            int x = m,sum = 0;
            while (x > 0)
            {
                sum + = x%10;
                x/=;
            }
            if (n = = sum+m)
                sheng[i++] = m;
        }
        if (sheng[0] = = 0)
            printf ("0\n");
        else//find the smallest generated element
        {
            int min = 100000;
            for (int j = 0;j < i;j++)
            {
                if (Sheng[j] < min)
                    min = sheng[j];
            }
            printf ("%d\n", min);
        }
    }
    return 0;
}

The second code is the AC code:

That's right... Array open big don't put int main inside ... Will explode stack

(for example, open 100005)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
using namespace std;

int dig[100005];
int main ()
{
    int t,n;
    memset (dig,0,sizeof (Dig));

    Computes the number of generators generated from 1 to 100005 for
    (int m = 1;m < 100005;m++)
    {
        int x = M,y = m;
        while (x > 0)
        {
            y + = x%10;
            x/=;
        }
        if (dig[y] = = 0| | M < dig[y])
            dig[y] = m;
    }

    scanf ("%d", &t);
    while (t--)
    {
        scanf ("%d", &n);
        printf ("%d\n", Dig[n]);
    }
    return 0;
}


Related Article

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.

Tags Index: