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)