Baidu interview questions to find an array that meets the conditions, Baidu interview questions Array

Source: Internet
Author: User

Baidu interview questions to find an array that meets the conditions, Baidu interview questions Array
Given the sum of the Functions d (n) = n + n, n is a positive integer, for example, d (78) = 78 + 7 + 8 = 93. In this way, this function can be regarded as a generator, for example, 93 can be regarded as generated by 78.
Define number A: Number A cannot find A number B can be created by d (B) = A, that is, A cannot be generated by other numbers. Now we need to write A program to find all the qualified numbers defined by A in 1 to 10000.
Answer:
Apply for a bool array with a length of 10000. Each element indicates whether other numbers can be generated for the corresponding value. At the beginning, all values in the array are initialized to false.
Because the number of generations greater than 10000 must be greater than 10000, we only need to traverse the numbers from 1 to 10000, calculate the number of generations, and set the corresponding value in the bool array to true, this number can be generated by other numbers.

The integer corresponding to the position where the value of the bool array is false cannot be generated by other numbers.

Below is the code I wrote.

# Include <iostream>
Using namespace std;
Void SCS (int n)
{
Int I;
Bool * a = new bool [n + 1];
Memset (a, false, n + 1 );
Int m = 0, s = 0, p = 0, q = 0;
For (I = 1; I <= n; I ++)
{
M = I % 10;
P = (I/10) % 10;
Q = (I/100) % 10;
S = (I/1000) % 10;
If (I + m + p + q + s <= n)
{
A [I + m + p + q + s] = true;
}
}
For (I = 0; I <= n; I ++)
{
If (a [I])
Cout <I <endl;
}
}
Int main ()
{
Int n;
Cout <"please cin the max num:" <endl;
Cin> n;
SCS (n );
Return 0;
}

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.