- 注册时间
- 2004-8-29
- 最后登录
- 1970-1-1
|

楼主 |
发表于 2005-5-29 11:14:00
|
显示全部楼层
第二题的代码:
- #include<stdio.h>
- #include<string.h>
- const int maxdepth=100;
- int depth=0,min,cmdepth; //depth:当前搜索深度-1,min:以求解中最后一个分母值,cmdepth:当前最大搜索深度
- int ans[maxdepth],out[maxdepth]; //ans :temp data,out:..
- void find(int a,int b,int x) //a/b, 1/x
- {
- if(depth>=cmdepth||x>=min) return;
- if(b%a==0){
- b/=a;
- if(b<x||b>=min) return;
- ans[depth]=b;
- min=b;
- memcpy(out,ans,(depth+1)*sizeof(int));
- return;
- }
- else{
- if(depth>=cmdepth-1) return;
- while(a*x<=b&&x<min) x++;
- while(x<min){
- if(a*x>=b*(cmdepth-depth)) break;
- ans[depth]=x;
- depth++;
- find(a*x-b,b*x,x+1);
- depth--;
- x++;
- }
- }
- return;
- }
- int main()
- {
- int a,b;
- scanf("%d%d",&a,&b);
- min=1000000;
- for(cmdepth=1;cmdepth<=maxdepth;cmdepth++){
- find(a,b,1);
- if(min<1000000) break;
- }
- for(int k=0;k<cmdepth-1;k++) printf("%d ",out[k]);
- printf("%d\n",out[cmdepth-1]);
- return 0;
- }
复制代码
用的是所谓的IDA*搜索算法。。。
|
|