很久以前的一份作业,似乎今年3月份时做的,都忘了思路了……
编程求某一个正整数的所有划分数。如输入6,则输出:
6 = 5+1
6 = 4+2
6 = 4+1+1
6 = 3+3
6 = 3+2+1
6 = 3+1+1+1
6 = 2+2+2
6 = 2+2+1+1
6 = 2+1+1+1+1
6 = 1+1+1+1+1+1
题目要求大致就是这样子,原题目要求是递归实现,但忘了为何我最后使用了迭代+链表实现了,反正不是自己要交的作业~ 代码如下~
有同学今年修了这门课,他来问我这道题,给他讲了递归版,顺手发上来吧。
递归版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include<stdio.h> #include<stdlib.h> int record[1001],total; int split(int n,int begin,int no) { int i,j; for(i=begin;i>0;i--) { if(i>n) { continue; } else if(i==n) { record[no]=i; printf("%d=%d",total,record[0]); for(j=1;j<=no;j++) { printf("+%d",record[j]); } printf("\n"); } else { record[no]=i; split(n-i,i,no+1); } } } int main() { int n,flag=0; scanf("%d",&n); total=n; split(n,n-1,0); } |
非递归(当时写的好奇怪……好繁琐)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
#include<stdio.h> #include<stdlib.h> int n,count=0; struct num { int number; struct num *next; struct num *pre; }*head; typedef struct num NUM; struct num_tree { NUM *num; struct num_tree *next; }*tree=NULL,*tree_head; typedef struct num_tree TREE; void free_nodes_after(NUM *node) { NUM *temp; while(node!=NULL) { temp=node; node=node->next; free(temp); } return ; } void print_num(NUM *node) { while(node!=NULL) { printf("%d",node->number); if(node->next!=NULL) printf("+"); node=node->next; } printf("\n"); return ; } NUM *copy(NUM *head,NUM *head_copied) { NUM *temp,*temp_copied,*temp_copied_head; temp=head; temp_copied_head=head_copied=NULL; while(temp!=NULL) { temp_copied=(NUM *)malloc(sizeof(NUM)); temp_copied->number=temp->number; if(temp_copied_head==NULL) { temp_copied->pre=NULL; temp_copied->next=NULL; temp_copied_head=head_copied=temp_copied; } else { temp_copied->pre=temp_copied_head; temp_copied->next=NULL; temp_copied_head->next=temp_copied; temp_copied_head=temp_copied; } temp=temp->next; } return head_copied; } void save_num_in_tree(NUM *node) { TREE *temp; NUM *node_copied; temp=(TREE *)malloc(sizeof(TREE)); node_copied=copy(node,node_copied); temp->next=NULL; temp->num=node_copied; if(tree!=NULL) tree->next=temp; else tree_head=temp; tree=temp; return ; } int count_num(NUM *head) { int sum=0; while(head!=NULL) { sum+=head->number; head=head->next; } return sum; } int do_split(NUM *node) { int i; NUM *temp; for(i=(node->pre)->number;i>0;i--) { node->number=i; if(n==count_num(head)) { print_num(head); if(i==1) { return ; } else { temp=(NUM *)malloc(sizeof(NUM)); temp->next=node->next; temp->pre=node; node->next=temp; do_split(temp); } } } } int do_another_split(NUM *node) { int i; NUM *temp,*temp_another; for(i=(node->pre)->number;i>0;i--) { node->number=i; if(n==count_num(head)) { print_num(head); // save_num_in_tree(head); if(i==1) { return ; } else { temp=(NUM *)malloc(sizeof(NUM)); temp->next=node->next; temp->pre=node; node->next=temp; do_another_split(temp); free_nodes_after(temp); node->next=NULL; } } else if(count_num(head)<n) { temp_another=(NUM *)malloc(sizeof(NUM)); temp_another->next=node->next; temp_another->pre=node; node->next=temp_another; do_another_split(temp_another); free_nodes_after(temp_another); node->next=NULL; } } } int main() { int i; FILE *fp; NUM *temp; scanf("%d",&n); head=(NUM *)malloc(sizeof(NUM)); for(i=n-1;i>0;i--) { temp=(NUM *)malloc(sizeof(NUM)); head->pre=NULL; head->next=temp; head->number=i; temp->pre=head; temp->number=0; temp->next=NULL; //getchar(); do_another_split(temp); free_nodes_after(temp); } /* fp=fopen("test.txt","w"); tree=tree_head; while(tree!=NULL) { temp=tree->num; //print_num(temp); fprintf(fp,"%d=",n); while(temp!=NULL) { fprintf(fp,"%d",temp->number); if(temp->next!=NULL) fprintf(fp,"+"); temp=temp->next; } fprintf(fp,"\n"); tree=tree->next; } fclose(fp);*/ return 0; } |