您好,欢迎来到保捱科技网。
搜索
您的当前位置:首页整数的分划问题

整数的分划问题

来源:保捱科技网


整数的分划问题。

如,对于正整数n=6,可以分划为: 6 5+1

4+2, 4+1+1

3+3, 3+2+1, 3+1+1+1

2+2+2, 2+2+1+1, 2+1+1+1+1 1+1+1+1+1+1+1

现在的问题是,对于给定的正整数n,编写算法打印所有划分。 用户从键盘输入 n (范围1~10) 程序输出该整数的所有划分。 #include #include struct node { int d; struct node *n; }*h,*p; int N,f;

void fun(int,int); void push(int); void pop(); void prf(); void main() { h=(struct node *)malloc(sizeof(struct node)); h->n=NULL; h->d=-1; p=h; printf(\"请输入一个1-10的数:\"); scanf(\"%d\ fun(N,N); }

void fun(int n,int sum) { int i; for(i=n;i>0;i--) { if(sum==N) f=0; if((sum-i)>0) { push(i); fun(i,sum-i);

pop(); } if((sum-i)==0) { push(i); if(f!=0) printf(\ prf(); f++; pop(); } if(sum==N) printf(\"\\n\"); } }

void push(int x) { struct node *q; q=(struct node *)malloc(sizeof(struct node)); q->n=NULL; q->d=x; p->n=q; p=q; }

void pop() { struct node *q; q=h; while(q->n!=p) { q=q->n; } p=q; q=p->n; p->n=NULL; free(q); }

void prf() { struct node *q; q=h->n; while(q!=NULL) { printf(\"%d\

}

}

q=q->n;

if(q!=NULL) printf(\"+\");

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baoaiwan.cn 版权所有 赣ICP备2024042794号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务