[Record] Luogu test field _ process and recursive function _ p1028, record_p1028

Source: Internet
Author: User

[Record] Luogu test field _ process and recursive function _ p1028, record_p1028

First way:

First try:

1 # include <bits/stdc ++. h> 2 using namespace std; 3 int k = 1; 4 void add (int s) 5 {6 if (s <= 1 | s/2 <= 0) return; 7 for (int I = 1; I <= s/2; I ++) 8 {9 add (I); 10 k ++; 11} 12} 13 int main () 14 {15 int n; 16 cin> n; 17 add (n); 18 cout <k; 19}

Result:

Timeout.

Second way:

First try:

1 # include <bits/stdc ++. h> 2 using namespace std; 3 int a [10001]; 4 int main () 5 {6 int n; 7 cin> n; 8 a [0] = 1; 9 for (int I = 1; I <= n; I ++) 10 for (int j = 0; j <= I/2; j ++) 11 a [I] + = a [j]; 12 cout <a [n]; 13 14}

Result:

Correct.

Reflection:Second wayAn array is used to simulate each sub-number that meets the conditions, and the call is directly output, with little time complexity.

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.

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.