| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2503 人关注过本帖
标题:[C语言编程接龙竞赛]第一题 设计一个N!的算法
只看楼主 加入收藏
乌鸦丘比特
Rank: 1
等 级:新手上路
威 望:2
帖 子:625
专家分:0
注 册:2004-7-19
收藏
得分:0 
#include <stdio.h>
/*可以调整MAXW来调整程序能承受的最大位数*/
#define MAXW 10000
int ans[MAXW];
void oprate(int n)
{/*a为每次要乘的数,up保存进位,max保存目前答案最大位数*/
int a,i,up,tmp,max;
ans[0]=1;up=0;max=1;
for(a=2;a<=n;a++)
{
for(i=0;i<max;i++)
{/*高精度乘法*/
tmp=ans[i]*a+up;
ans[i]=tmp%10;
up=tmp/10;
}

while(up)
{
ans[i++]=up%10;
up/=10;
}
max=i;/*新的最高位*/
}
while(max--)
printf("%d",ans[max]);
printf("\n");
}
int main(void)
{
int n;
while(scanf("%d",&n)==1)
{/*输入任意数字字符程序即结束*/
oprate(n);
}
return 0;
}

我喜欢创造,一只扑腾着翅膀向天空飞翔的乌鸦
2005-10-29 21:28
noback
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-10-9
收藏
得分:0 

真是长见识了。我运行了一下,错误有一点点,就是重复定义了一些变量,还有就是变量的作用域不对。
改正后如下:
#include <stdio.h>
#include <conio.h>


/*
-- Program detail -----------
Programmer : yuki
Last modify date: 2005-10-29
-----------------------------
*/

/*
NOTICE: If you want to placed this code to another bbs, please note the origin
*/

/* Macro definition as buffer size */
#define BUFFER_SIZE 3000
#define DELIMITER_WIDTH 4

/* Holding the result */
static int array[BUFFER_SIZE] = {0};
/* Holding the data length */
static int vaild_bit = 1;

int main() {
int N;
int i = 1,j,temp;
printf("Input N = ");
scanf("%d",&N);
/* Judge the data which inputed by user */
if(N < 0 || N > 1000) {
printf("Impossible!\n");
getch();
return 0;
}
else {
/* Set the base number as 1 */
array[0] = 1;
/* Integer varible i and j functioned as counter, temp using to calc */
/* General mutiplication */
while(i <= N) {
for(j = 0; j < vaild_bit; ++j) array[j] *= i;
for(j = 0; j < vaild_bit; ++j) {
if(array[j] < 10 && j == vaild_bit-1) break;
else if(array[j] < 10) continue;
temp = array[j];
array[j] = temp % 10;
array[j+1] += temp / 10;
/* Extend a bit for a new result */
if( array[j+1] >= 10 && j == vaild_bit-1 ) ++vaild_bit;
}
/* Reset the length of new data */
vaild_bit = j + 1;
++i;
}
/* Result published */
printf("%d!(%d bit(s)) = \n",N,vaild_bit);
for( i = vaild_bit - 1; i >= 0; --i ) {
printf("%d",array[i]);
/* Every four figures delimited by a space */
if(!(i % DELIMITER_WIDTH)) putch(0x20);
}
printf("\n");
getch();
}
return 0;
}


2005-10-29 21:37
amoeba
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2004-8-9
收藏
得分:0 
noback,

yuki用的是C99标准的编译器,你所说的那些问题是C89与C99的差异。希望楼上的各位都写出自己的代码,不要怕写得差,要把自己的代码展示出来,有比较才知道自己的长短处。

2005-10-29 21:53
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

我改进了一下,削减了while中的一个for语句
我使用TC3.0和Dev C++均能通过编译

#include <stdio.h>
#include <conio.h>

/*
-- Program detail -----------
Programmer : yuki
Last modify date: 2005-10-29
-----------------------------
*/

/*
NOTICE: If you want to placed this code to another bbs, please note the origin
*/

/* Macro definition as buffer size */
#define BUFFER_SIZE 2568
#define DELIMITER_WIDTH 4

/* Holding the result */
static int array[BUFFER_SIZE] = {0};
/* Holding the data length */
static int vaild_bit = 1;

int main() {
int N;
printf("Input N = ");
scanf("%d",&N);
/* Judge the data which inputed by user */
if(N < 0 || N > 1000) {
printf("Impossible!\n");
getch();
return 0;
}
else {
/* Set the base number as 1 */
array[0] = 1;
/* Integer varible i and j functioned as counter, temp using to calc */
register int i = 1,j,k,temp = -1;
/* General mutiplication */
while(i <= N) {
for(j = 0; j < vaild_bit; ++j) {
array[j] *= i;
if(temp >= 0) {
array[j] += temp/10;
temp = -1;
}
if(array[j] < 10 && j == vaild_bit-1) break;
else if(array[j] < 10) {
temp = -1;
continue;
}
temp = array[j];
array[j] = temp % 10;
/* Extend a bit for a new result */
if( temp+array[j+1]*(i+1) >= 10 && j == vaild_bit-1 ) ++vaild_bit;
}
/* Reset the length of new data */
vaild_bit = j + 1;
++i;
}
/* Result published */
printf("%d!(%d bit(s)) = \n",N,vaild_bit);
for( i = vaild_bit - 1; i >= 0; --i ) {
printf("%d",array[i]);
/* Every four figures delimited by a space */
if(!(i % DELIMITER_WIDTH)) putch(0x20);
}
printf("\n");
getch();
}
return 0;
}

下面附一个阶乘结果位数估计的方法:
对于n!,我们可以得其结果的长度便是log(1)+log(2)+log(3)+...+log(n),然后取整就是该阶乘结果位数的估计值。


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2005-10-29 22:36
84819986
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2005-10-30
收藏
得分:0 
都是强人啊  佩服佩服 我就不献丑了!!!
2005-10-30 10:29
ghy2001
Rank: 1
等 级:新手上路
威 望:1
帖 子:87
专家分:0
注 册:2005-10-30
收藏
得分:0 
#include<stdio.h>
void main()
{
int i,n,all;
printf("enter n\n");
scanf("%d",&n);
if(n<=1000&&n>=0)
{
all=1;
for(i=0;i<n;i++)
{
all=all*(i+1);
}
printf("%d!=%d\n",n,all);
}
else
printf("error");
}

你们的怎么那么长?

2005-10-30 12:05
unicorn
Rank: 4
等 级:贵宾
威 望:14
帖 子:1066
专家分:0
注 册:2005-10-25
收藏
得分:0 

嘿嘿 当然不是说越长越好 毕竟得考虑结果的范围才行啦
乌鸦丘比特给的算法非常好!语句运行起来的思路很清晰,又没有漏洞!佩服!


unicorn-h.spaces. ◇◆ sava-scratch.spaces.  noh enol ! pue pu!w hw u! shemle aq ll!m noh 
2005-10-30 14:50
Fudan_Men
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2005-9-14
收藏
得分:0 

这种题目找本书抄来就可以……我的那本教材上就有这个利用数组代替自然数位数的算法……还是编写大的实用的程序吧!

2005-10-30 15:59
Knocker
Rank: 8Rank: 8
等 级:贵宾
威 望:47
帖 子:10454
专家分:603
注 册:2004-6-1
收藏
得分:0 
此题难度偏大了一点,有很多想参与的没参与,提前两小时结贴。

共有三人按时提交了程序:

yuki

乌鸦丘比特

ghy2001

九洲方除百尺冰,映秀又遭蛮牛耕。汽笛嘶鸣国旗半,哀伤尽处是重生。     -老K
治国就是治吏。礼义廉耻,国之四维。四维不张,国之不国。   -毛泽东
2005-10-30 20:23
Knocker
Rank: 8Rank: 8
等 级:贵宾
威 望:47
帖 子:10454
专家分:603
注 册:2004-6-1
收藏
得分:0 

九洲方除百尺冰,映秀又遭蛮牛耕。汽笛嘶鸣国旗半,哀伤尽处是重生。     -老K
治国就是治吏。礼义廉耻,国之四维。四维不张,国之不国。   -毛泽东
2005-10-30 22:00
快速回复:[C语言编程接龙竞赛]第一题 设计一个N!的算法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015125 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved