Huawei Machine Question "9"-integer divided into 2 power times

Source: Internet
Author: User

Title Description:

An integer can be split into the sum of 2 powers, for example:

7 = 1+ 2 +
4

7 = 1 + 2
+ 2 + 2

7 = 1 + 1
+ 1 + 4

7 = 1 + 1
+ 1 + 2 + 2

7 = 1 + 1
+ 1 + 1 + 1 + 2

7 = 1 + 1
+ 1 + 1 + 1 + 1 + 1

There are six different ways to split

Another example: 4 can be split into:
4 = 4, 4 = 1+1+1+1, 4 = 1+1+2, 4 =.

Use f (n) to denote the number of different splits of N, e.g. F (7)
= 6.

Requirements:

Required to read n (not more than 1000000), output f (n)
% 1000000000.

Thinking Analysis:
    • This topic has no direct formula to use, consider recursion
    • Using the idea of dynamic programming
    • When that 2k +1,f (2k+1) certainly contains a 1,f (2k+1) =f (2k)
    • n is even when the split is divided into 1 and does not contain 12 cases, f (2k) = f (2k-2) +f (k), containing 1 of the case, must contain at least two 1, not including 1 is divided by 2, is f (k)
    • Also note the output to% 1000000000, note the remainder of the nature: (a+b)%m = (a%m+b%m)%m, so as long as each intermediate result also take the remainder, there will be no overflow problem, and will not change the final output results
    • -
Code:
Import Java.util.Scanner; Public classMain {Static int[] num = {1,2,5,Ten, -, -, -}; Public Static void Main(string[] args) {Scanner scan =NewScanner (System.inch); while(Scan.hasnext ()) {intinput = Scan.nextint ();if(Input <1){return; } System. out. println (GetNumber (input)); }       } Public Static int GetNumber(intN) {if(n = =1){return 1; }if(n = =2){return 2; }if(n%2!=0){returnGetNumber (n1)%1000000000; }Else{returnGetNumber (n2)%1000000000+getnumber (n/2)%1000000000; }    }}
My QR code is as follows, welcome to exchange discussion

You are welcome to pay attention to the "It question summary" subscription number. Every day to push the classic face test and interview tips, are dry! The QR code of the subscription number is as follows:

      -

Huawei Machine Question "9"-integer divided into 2 power times

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.