title Construction expression category comprehensive time limit 1S Memory limit 100Kb Problem description Give an integer n representing the length of the sequence (3 < = n < = 9). Insert '+', '-', '' in the sequence 1, 2, 3... N to construct the expression. Inserting '' means that the first and second digits form an integer, for example, 1, 2 - 3 - 4 - 5 = 0. Output the number of expressions with a result of 0 in all expressions constructed. For example, when n=3, only expression 1 + 2-3 = 0 and the output result is 1. Enter description The input data is an integer n (n < 10), which indicates the length of the sequence, and the input sequence is "1, 2, 3... N". Output description For each group of data, an integer is output to represent the number of expressions with a result of 0 in the constructed expression. sample input 3 sample output 1
The problem was stuck for two days.
The last idea comes from grandsleeper, the senior student of the last big man,
Publish a problem solution again and review the problem.
#include<stdio.h> int a[20]={1,1}; int head,tail; int point =1; int n; int ans=0; int main(){ void dfs(int ); scanf("%d",&n); dfs(1); a[1]=1; printf("%d",ans); return 0; } void dfs(int point){ int i; for(i=0;i<3;i++){ if(point==n){ break; } if(i==1){ point++; tail=point; head=tail; a[point]=point; dfs(point); a[point]=0; point--; tail=point; if(head>tail) head=tail; continue; } if(i==2){ point++; tail=point; head=tail; a[point]=-point; dfs(point); a[point]=0; point--; tail--; if(head>tail) head=point; continue; } //Indicates plus and minus signs int p=head,q=tail; //This is different from the boss handling. p and q are used to temporarily store the head and tail. When the head and tail are placed in the next level dfs call, the head and tail are shifted //QAQ changed this for two hours //Indicates that the middle is left blank if(i==0){ point++; int len=1; tail=point; while(a[head-1]>=10||a[head-1]<=-10){ head--; } p=head,q=tail; if(a[head]>0){ a[point]=point; } else { a[point]=-point; } int j; for(j=head;j<tail;j++){ a[j]*=10; } // a[point-1]=0; } int j; dfs(point); a[point]=0; for(j=p;j<q;j++){ a[j]/=10; } point--; tail=point; if(head>tail) head=tail; continue; } int sum=0; for(i=1;i<=n;i++){ sum+=a[i]; // printf("%d ",a[i]); } if(sum==0&&a[n]!=0){ ans++; } // printf("%d ",sum); // printf("\n"); sum=0; }
The general idea of this question is to call dfs and restore it. Writing this question as a whole is the day of collapse.
But there are gains.
Thank you for your article. Paste a link here (the general idea is the same, but there are some problems in the last space) Xdoj five-star question 172 construction expression (recursive idea) _grandsleepblog CSDN blog xdoj five-star question 127 construction expression Title Construction expression category comprehensive time limit 1S memory limit 100Kb problem description given an integer n representing the length of the sequence (3 < = n < = 9). Insert '+', '-', '' in the sequence 1, 2, 3... N to construct the expression. Inserting '' means that the first and second digits form an integer, for example, 1, 2 - 3 - 4 - 5 = 0. Output the number of expressions with a result of 0 in all expressions constructed. For example, when n=3, only expression 1 + 2-3 = 0 and the output result is 1. Input description the input data is an integer n (n < 10), which indicates the length of the sequence and the input sequence is "1" https://blog.csdn.net/grandsleeper/article/details/111772164
At the same time, you can take a look at this article to get an introduction to dfs dfs entry level (template) floating life - CSDN blog dfs entry dfs has been writing the title of the Blue Bridge Cup recently, many of which involve search (dfs, BFS), etc. because recursion is too abstract, they can't master it well. So they wrote this introductory tutorial to deepen their understanding of dfs and fully understand recursion. The so-called dfs refers to giving priority to depth, in other words, when one road goes dark, they won't choose to turn back until there is no way to go. Then they choose another road. 1. Full arrangement (Introduction) guide question: input a number n and output the full arrangement of n. you can visualize this problem first. For example, if there are three playing cards numbered 1,2,3 and three boxes numbered 1,2,3, put the three playing cards into three boxes respectively, and there are several different ways to put themhttps://blog.csdn.net/m0_46549425/article/details/108025133?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164050942616780274199973%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=164050942616780274199973&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-108025133.first_rank_v2_pc_rank_v29&utm_term=dfs&spm=1018.2226.3001.4187 In addition, you guys, there is another question about konjak. Why don't you add a [n] in the final statistics= 0, there will be many more ans. I hope you can give me more advice.