Ruby,Block,CPS 和 call/cc
2008-01-03 09:39
323 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
标题:<<系统设计师>>应试编程实例-[递推算法程序设计]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
作者:成晓旭
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
时间:2002年09月11日(11:52:00-16:26:00)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
实现递推算法的大整数阶乖处理函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
时间:2002年09月16日(18:38:00-20:02:00)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
实现“斐波那契数列”问题的递推算法函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdio.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdlib.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//:============================“大整数阶乖”问题的递推算法===========================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAXN 1000 //最大数据位数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//用递推法求取整数k的阶乖,将结果放入数组array中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void pnext(int array[],int k)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int *temp; //动态数组[临时存储运算大整数]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i,j,num_len = array[0],carry,t; //循环变量,长整数位数,进位标志,临时变量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(array[0] >= MAXN)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("数据处理位数超过程序设计上限,程序将自动中断运行! ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
exit(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
temp = (int *)malloc(sizeof(int) * (num_len + 1)); //创建动态数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i=1;i<=num_len;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
temp[i] = array[i]; //保存原始数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(j=1;j
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(carry = 0,i=1;i<=num_len;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(i <= array[0])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
t = array[i] + temp[i] + carry;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
t = array[i] + carry; //处理最高位
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//数据位调整
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
array[i] = t % 10;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
carry = t / 10;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(carry)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
array[++num_len] = carry; //在最高位记录进位标志
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
free(temp);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
array[0] = num_len;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//显示阶乖结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Show_Result(int array[],int base_number)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%4d!=",base_number);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i=array[0];i>0;i--)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%d",array[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//计算数据的阶乖
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Count_Result(int array[], int base_number)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int k;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
array[0] = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
array[1] = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(k=2;k<=base_number;k++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pnext(array,k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Show_Result(array,k);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//:============================“大整数阶乖”问题的递推算法===========================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//:============================“斐波那契数列”问题的递推算法===========================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
问题描述:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
标准斐波那契数列:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
F(0) = 0,F(1) = 1,Fn) = F(n-2) + F(n-1)(当n > 1时)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
广义斐波那契数列:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
F(0) = 0,F(1) = 0,F(2) = 1,Fn) = F(n-3) + F(n-2) + F(n-1)(当n > 2时)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//标准斐波那契数列的递推算法函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int Std_Fibonacci(int number)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int f0 = 0,f1 = 1,result,loop; //初始值1,2,返回结果,循环计数器
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(number == 0) return(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(number == 1) return(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(loop = 2;loop <= number;loop ++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result = f0 + f1; //由前两步的结果计算当前结果--->“推出”
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
f0 = f1; //把原来的前一步当作下一次的前两步--->“传递”
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
f1 = result; //把当前结果当作下一次的前一步--->“传递”
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//注意:在进行这种向前传递的操作时,特别小心传递的时序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(result);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//广义斐波那契数列的递推算法函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int Ext_Fibonacci(int number)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int f0 = 0,f1 = 0,f2 = 1,result,loop; //初始值1,2,3,返回结果,循环计数器
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(number == 0 || number == 1) return(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(number == 2) return(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(loop = 2;loop <= number;loop ++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result = f0 + f1 + f2; //由前三步的结果计算当前结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
f0 = f1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
f1 = f2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
f2 = result;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//注意:在进行这种向前传递的操作时,特别小心传递的时序[即:变量间赋值的前后关系及先后顺序]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(result);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Run_Fibonacci()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int large,result;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(large!=9910)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" 请输入斐波那契数列的元素个数[Enter '9910' to Exit]: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d",&large);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(large!=9910)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//result = Std_Fibonacci(large);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result = Ext_Fibonacci(large-1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("斐波那契数据中的第[%d] = %d ",large,result);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//:============================“斐波那契数列”问题的递推算法===========================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int LargeArray[MAXN],large = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(large!=9910)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("请输入需要求取其阶乖的整数[Enter '9910' to Exit]: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d",&large);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(large!=9910)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Count_Result(LargeArray,large);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Run_Fibonacci();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" 应用程序运行结束! ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=935912
相关文章推荐
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Ruby,Block,CPS 和 call/cc
- Scheme的Lambda与Ruby的Block
- cocos2d-x中CCCallFunc CCCallFuncN CCCallFuncND的区别和使用示例
- 强烈推荐,非常好的一篇关于call/cc的文章,非常简单的英文。
- CCCallFunc、CCCallFuncN和CCCallFuncND