Digit Generator UVA-1583

Source: Internet
Author: User

Digit Generator UVA-1583

Topic Portal:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 4458

The main idea: Generate a meta, the value of a number x is another number y value plus y of the sum of the number of members;
216 = 198 + 1 + 9 + 8;
2005 = 1979 + 1 + 9 + 7 + 9;

Analysis: Are we going to enumerate all the numbers that are smaller than x, and compare them to see which one is its generator?
This is too complicated, time-consuming and laborious;

Another way is to make a table, yes first let the background compiler to preprocess a batch of data, and then we directly check the table is good;

How to make a watch.
We'll start by test instructions a table of size 10086;

int ans[10086];
for (int i = 0; i < 10086; i++)//full
    int x = i, y = i;
    while (x > 0) {y + = x%10; x/= 10}//current digit x is taken apart and added, note that it is added on the basis of Y, because we are directly going to achieve the purpose of "ans[y] = Y's generator";
    if (ans[y] = = 0 | | I < ans[ Y])//assignment, and according to the test instructions to small (we I from small to big add up, so there is no need to judge the small, but add to complete)
        ans[y] = i;

At this time, ans[] is a form that meets our requirements;

There is a lot of data is very large, but also need to query the comparison of the topic, you can pre-preprocess a table to solve, will save a lot of time, efficiency significantly increased;

The following is an AC Code:

 #include <bits/stdc++.h> using namespace std; int ans[100086];
        void MakeList ()//Hit table {for (int i = 0; i < 100086; i++) {int x = I,y = i;
            while (x > 0) {y + = x%10;
        x/= 10;
    } if (ans[y] = = 0 | | ans[y] > i) ans[y] = i;
    }} int main () {int m,n;
    memset (ans,0,sizeof (ans));
    MakeList ();
        while (n--) {cin>>m;
} 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: