current position:Home>C language beginners' doubts, no, require recursion
C language beginners' doubts, no, require recursion
2022-02-03 01:07:55 【CSDN Q & A】
For any positive integer n, You can always find positive integers and see k1、k2…, Make these numbers want to add up just equal to n, And each number does not exceed the specified number m.
ask : For a given n, Please find out how many such summations there are
Be careful ;
set up a,b It's not equal , Think a+b And b+a Is a different summation ;
n=n It's also a summation
for example :
n=4,m=2.
4=1+1+1+1
4=1+1+2
4=1+2+1
4=2+1+1
4=2+2
Refer to the answer 1:
#include <stdio.h>int cnt=0;void comb(int a[],int m,int k,int s) { int i,j,t; for(i=m;i>=k;i--) { a[k]=i; if(k>1) comb(a,i-1,k-1,s); else { for(t=0,j=a[0];j>0;j--) t=t+a[j]; if(t==s) { cnt++; printf("%d=", s); for(j=a[0];j>1;j--) printf("%d+", a[j]); printf("%d\n", a[1]); } } }}int main(){ int a[100],s,m,i; while (1) { scanf("%d %d", &s, &m); if (s == 0) { break; } cnt=0; for (i=1;i<=m;i++) { a[0]=i; comb(a,m,i,s); } printf("%d\n", cnt); } return 0;}
Running results :
Refer to the answer 2:
Refer to the answer 3:
“ Given a small point of input , Full single step tracking ( Press at the same time Alt+7 Key view Call Stack The corresponding function call history from the inner layer to the outer layer is listed from top to bottom ) Again .” Is the only way to understand the working principle of recursive functions !
Recursive functions focus on the following factors
· Exit conditions
· What are the parameters
· What is the return value
· What are the local variables
· What are the global variables
· When to output
· Will it cause stack overflow
For reference only :
#include <stdio.h>#include <stdlib.h>void print(int res[], int num) { static int L=0; L++; printf("%8d:",L); for (int i=0;i<num;++i) { printf(" %d", res[i]); } printf("\n");}void split(int n, int m) {
// n Represents the total number ,m Represents the maximum factor static int res[100];// Save results static int num=-1;// Current factor subscript if (n<m || n<0 || m<1) return; num++; if (0==n) {
// Recursive termination condition , by 0 Can not be further divided , Direct output print(res,num+1); num--; return; } else { if (n==m) {
// Don't dismantle , Direct output res[num]=m; print(res,num+1); num--; } else { // Split the first res[num]=m; n=n-m; if (m>n) m = n; // The maximum factor cannot be greater than the total for (int i=m;i>=1;--i) {
// loop , The second factor can continue to split , And it can be divided into multiple according to the maximum factor split(n,i); } num--; } }}void Split(int n) { if (n<=0) return; if (100<n) { printf("Up to 100\n"); return; } for (int i=n;i>=1;--i) { split(n, i); }}void main(int argc,char **argv) { if (argc<=1) Split(5); else if (argc>=3) split(atoi(argv[1]),atoi(argv[2])); else Split(atoi(argv[1]));}
Refer to the answer 4:
copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030107519801.html
The sidebar is recommended
- Java byte array and string conversion
- Definition of two-dimensional array in C language
- Why is there an extra 0 here?
- Python code programs don't
- Measure the distance with sound wave, and then send the result to the serial port. Do you want to interrupt twice, language interrupt, and ask for an idea
- An error occurs when starting Hadoop. How to solve it?
- What about the problem of remote access server?
- pywintypes. com_ Error: (- 2147023728, 'element not found', none, none)
- Global variables don't work in this program? Why is the answer a
- In Python, there is something wrong with the program and an error is reported
guess what you like
-
Simple OJ questions for Freshmen
-
Butterfly effect problem, insufficient number of input parameters
-
Use of extractall function of Python zipfile module
-
After spring imports the database and c3p0 dependency package in idea, it will prompt the database for abnormal error
-
Python from introduction to practice 5-10
-
1703792 what does that mean? What's wrong?
-
C language and C + are easy to search dogs
-
This interface is always used when I use CONDA to install tensorflow? About half an hour. How?
-
React summernote rich text editor!! Why do some function icons of rich text editor fail after I pack them
-
How does silly girl robot log in with mobile phone
Random recommended
- Why can't I see the absolute path of the command
- Pycharm uses the requests crawler to report the wrong requests exceptions. SSLError: HTTPSConnectionPool
- How to run multiple sleep () simultaneously in C language
- The data requested by Axios in vue3 is put into ecarts, which always shows that setoption is not defined
- What do these two for loops mean
- Java judges whether there are 30 consecutive int arrays with a length of 1000 as the growth trend
- Force deduction 221 question maximum square
- When I set this layout, I prompted an error from line 18
- The import javax. security. auth. message cannot be resolved
- The hottest Christmas tree code. one hundred and twenty-three
- Open the edge browser and display "what if I can't access this page?"?
- Running problems of java simple supermarket system
- Is the data [10] here an address or an element? The concept is always unclear
- Python crawling news content
- How to deal with the problem of general code framework
- Bubble sort, how can there be an extra number in front
- The graph drawing algorithm of Python is described by Raptor flow chart software
- Numpy creates an array of the specified data range
- Python crawler obtains the data of Douban movie top250 fantasy class
- R7-2 minimum spanning tree construction (25 points)
- Using burpsuite to capture the package of Pikachu shooting range SQL to inject the first level error
- Jar package conflict. I don't know where 2.29.1 came from
- About the rotation matrix on leetcode!
- The raspberry pie prompts cannot find webcams when running the command
- C + + a simple encryption, look at the warning how to modify, output nothing
- PHP composer and Vue node_ modules
- JDBC connects to the database for data deletion. The syntax is correct, but the data in the database cannot be deleted
- Java interface class program, define an interface and define two classes to implement the interface, then use the sorting method in the class, then enter an array with length of 8 in main, and call newsort with the two implementation classes as arguments
- Sort with average score, preferably bubble method
- When running the service jar, report Linux 1] + exit 1
- How to solve the problem of traceback? (Language Python)
- If an undirected graph G has 100 edges, how many vertices does the undirected graph have at least
- There was a problem calling the custom function
- The sum of a positive integer and 3 is a multiple of 5, and the difference between a positive integer and 3 is a multiple of 6. Write a program to find the minimum number that meets the conditions.
- Oracle has customized a date function. What's wrong with this writing?
- Spring boot project failed to introduce echarts
- How to solve this kind of program
- How to understand this? I didn't understand it at the beginning
- This problem requires the implementation of a function to reverse the given one-way linked list, that is, the header is set to the end of the table, and the end of the table is set to the header.
- How should this program be completed