6. Calculation of the number (recursive algorithm)

Source: Internet
Author: User
Tags pow

Calculation of the number (recursive algorithm)

Time limit: 1 s

Space limit: 128000 KB

Title Level: Silver

Exercises

View Run Results

Title Description Description

We need to find out the number of characters with the following properties (the natural number of inputs N):

First enter a natural number n (n<=1000), and then treat this natural number as follows:

1. Do not make any treatment;

2. Add a natural number to the left of it, but the natural number cannot exceed half of the original number;

3. After adding a number, continue to follow this rule until no more natural numbers can be added.

Enter a description Input Description

A number n

Output Description Output Description

Number of numbers that satisfy the condition

Sample Input Sample Input

6

Sample Output Sample Output

6

data range and tips Data Size & Hint

The 6 numbers were:

6

16

26

126

36

136

Program One:

You can find the total and each number.

#include

using namespace Std;

#include

Char a[5];

#include

int n,l;

int p[100],t=0;

int pow (int x,int n)

{

int t=1;

for (int i=1;i<=n;++i)

T*=x;

return t;

}

void input ()

{

scanf ("%s", a);

L=strlen (a);

for (int i=l-1,j=0;i>=0;i--, j + +)

n+= (a[i]-' 0 ') *pow (10,J);

}

void search (int i,int n,int L1)

{

P[++t]=i*pow (10,L1) +n;//Note pow function pow (10,2) =99

for (int j=1;j<=i/2;j++)

Search (J,i*pow (10,L1) +n,l1+1);

}

int main ()

{

Input ();

P[++t]=n;

for (int i=1;i<=n/2;++i)

Search (i,n,l);

printf ("%d\n", t);

for (int i=1;i<=t;++i)

printf ("%d\n", P[i]);

return 0;

}

Program Two:

Find only the total

#include

using namespace Std;

#include

Char a[5];

#include

#include

int n,l;

int p[100],t=0;

void input ()

{

scanf ("%s", a);

L=strlen (a);

for (int i=l-1,j=0;i>=0;i--, j + +)

n+= (a[i]-' 0 ') *pow (10,J);

}

void search (int i,int n,int L1)

{

++t;//Note Pow function pow (10,2) =99

for (int j=1;j<=i/2;j++)

Search (J,i*pow (10,L1) +n,l1+1);

}

int main ()

{

Input ();

++t;

for (int i=1;i<=n/2;++i)

Search (i,n,l);

printf ("%d\n", t);

return 0;

}

6. Calculation of the number (recursive algorithm)

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.