Count-recursive algorithm

Source: Internet
Author: User

Problem description We need to find the number (including the natural number N of the input) with the following number of properties ). Enter a natural number n (n <= 1000), and then process the natural number as follows:
No processing;
Add a natural number to its left, but the natural number cannot exceed half of the original number;
After adding the number, continue to follow this rule until the natural number cannot be added.
Input The input contains multiple groups of data, each of which is a natural number of N. Output The number of data outputs in each group that meet the conditions. Sample Input
 
6
Sample output
 
The number of 6 hint conditions is 6, 16, 26,126, 36,136.
Author Hynu

Code:

 
// Recursive formula: H (I) = H (I-1) When I is an odd number; H (I) = H (I-1) When I is an even number) + H (I/2 ). # include <stdio. h> int H [1001]; int fun (int n) {int I; H [1] = 1; for (I = 2; I <= N; I ++) {if (I % 2 = 1) h [I] = H [I-1]; else h [I] = H [I-1] + H [I/2];} return H [N] ;}int main () {int N; while (scanf ("% d", & N )! = EOF) {printf ("% d \ n", fun (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.