Ka的回溯编程练习 Part1|整划什么的。。

 1 #include<stdio.h>
 2 int search(int s,int t);
 3 void op(int k);
 4 int res[1001]={1},n;
 5 int main()
 6 {
 7     //scanf("%d",&n);
 8     n=10;
 9     search(n,1);
10     return 0;
11 }
12 int search(int s,int t) //当前数的大小s,个数n 
13 {
14     int i;
15     for(i=res[t-1];i<=s;i++) //i要比前一个数大,且在剩余的s值中寻找 
16     {
17         if(i<n)    //理论上来说s<n  所以i<n可以写为i<=s 但是这并没有什么卵用 
18         {
19             res[t]=i;
20             s-=i;
21             if(s==0) op(t);
22             else search(s,t+1);
23             s+=i;
24         }
25     }
26 }
27 void op(int k)
28 {
29     int i;
30     printf("<%d>=",n);
31     for(i=1;i<=k-1;i++)
32         printf("%d+",res[i]);
33     printf("%d\n",res[i]);
34         
35 } 

的确。。

posted on 2015-05-16 21:15  Ricochet!  阅读(124)  评论(0编辑  收藏  举报