C语言常见算法——求大数阶乘!
发布时间:2020-12-14 04:12:20 所属栏目:大数据 来源:网络整理
导读:注意点: 1 是大数的阶乘 2 注意会溢出 /************************************************************************* ??? File Name: Factorial.c ??? Author: hai--feng ??? Mail: haifeng@126.com ??? Created Time: Tue 26 Mar 2013 08:11:03 AM CST ?*
注意点: 1 是大数的阶乘 2 注意会溢出 /************************************************************************* ??? > File Name: Factorial.c ??? > Author: hai--feng ??? > Mail: haifeng@126.com ??? > Created Time: Tue 26 Mar 2013 08:11:03 AM CST ?************************************************************************/ #include <stdio.h> void Factorial( int val ) { ??? int Data[ 10000 ] = { 0 }; ??? int media = 1;????????????? //有效数据个数 ??? int ii = 0,ij = 0; ??? if( 0 > val ) ??? { ??????? printf( "param is illegal!n" ); ??? } ??? else ??? { ??????? if( val == 0 )??? //0的阶乘为1 ??????? { ??????????? Data[ media - 1 ] = 1; ??????? } ??????? else ??????? { ??????????? for( ii = 1; ii <= val; ii++ )??? //循环val次 ??????????? { ??????????????? for( ij = 0; ij < media; ij++ )???? //每一次对于当前数据求阶乘 ??????????????? { ??????????????????? if( ( 1 == media ) && ( 1 == ii ) )?? //如果是1的阶乘 ??????????????????? { ??????????????????????? Data[ ij ] = 1; ??????????????????? } ??????????????????? else ??????????????????? { ??????????????????????? Data[ ij ] *= ii;??? //求每一个单元的值 ??????????????????? } ??????????????? } ??????????????? for( ij = 0; ij < media; ij++ )??????? //进位 ??????????????? { ??????????????????? if( ( ij < ( media -1 ) ) && ( Data[ ij ] >= 10 ) )?????? //如果在当前的有效数据范围内部 ??????????????????? { ??????????????????????? Data[ ij + 1 ] += Data[ ij ] / 10; ??????????????????????? Data[ ij ] %= 10; ??????????????????? } ??????????????????? else if( ( ij == ( media -1 ) ) && ( Data[ ij ] >= 10 ) )???? //如果在当前的有效数据范围外部,有效数据个数++ ??????????????????? { ??????????????????????? Data[ media ] += Data[ ij ] / 10; ??????????????????????? Data[ ij ] %= 10; ??????????????????????? media++; ??????????????????? } ??????????????? } ??????????? } ??????? } ??????? for( ii = ( media - 1 ); ii >= 0; ii-- ) ??????? { ??????????? printf( "%2d",Data[ ii ] ); ??????? } ??????? printf( "n" ); ??? } } int main(void) { ??? int factorial = 0; ??? while( 1 ) ??? { ??????? printf( "enter number:" ); ??????? scanf( "%d",&factorial ); ??????? Factorial( factorial ); ??? } ?? ? ??? return 0; } 思想:? 如n!,循环次数为n,对于每一次循环,对应位算出结果,然后算出对于10的余数,其商进位。(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何在vb.net中获取ListView的SelectedItem或SelectedIndex
- delphi – 即使UI状态不说明,TLabel也会显示加速键
- LightOJ 1370 Bi-shoe and Phi-shoe【欧拉打表】
- VSLua:将Lua语言无缝结合到Visual Studio当中 调试lua ,高亮
- ulink,mkpath,rmtree
- LUA学习(四)函数
- 4月19日-4月25日 记
- coco2dx + lua 新建工程 error LNK2019: 无法解析的外部符号
- Spring Boot Data Rest POST返回204但仅返回SELECTS
- golang(2):beego 环境搭建