NEFU560 Half-Number set "recursion"

Source: Internet
Author: User

Topic Links:

http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=560


Main topic:

Given a natural number n, there is n starting to produce a set of half sets (n). Set (N) is defined as follows:

1) N is the element in set (n)

2) The natural number on the left side of N, but the natural number cannot exceed the half of the most recently added number.

3) Follow this rule until the natural number cannot be added.

For example: N = 6 o'clock, only the natural number not exceeding 6/2=3 is 1, 2, 3, which is 16, 26, 36. and 26,

36 You can continue to add 1, that is, 126, 136. Then Set (N) = {6, 16, 26, 126, 36, 136}.


Ideas:

Recursive addition, the number of Halfset (N) can be added, traverse 1~n/2,ans + = Halfset (i), pass

Get the answer. But this obviously repeats many of the same problems, using the array shu[] to save the results of the calculation, handing

When the result is present, the result value can be returned directly or the result value will be returned first.


AC Code:

#include <iostream>//#include <algorithm>//#include <cstdio>//#include <cstring>//using namespace Std;////int halfset (int n)//{//int ans = 1;//if (N > 1)//for (int i = 1; I <= n/2; ++i)//ans + = Halfset (i); /return ans; }////int Main ()//{//int N;//while (~scanf ("%d", &n))//{//printf ("%d\n", Halfset (N));//}////return 0;//} # include<iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std ; int shu[11000];int halfset (int N) {int ans = 1;if (Shu[n]) return shu[n];for (int i = 1; I <= n/2; ++i) ans + = Halfset (i); r Eturn Shu[n] = ans;} int main () {int n;while (~scanf ("%d", &n)) {printf ("%d\n", Halfset (N));} return 0;}


NEFU560 Half-Number set "recursion"

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.