主题:程序结果有问题
天涯行客
[专家分:0] 发布于 2010-11-19 20:26:00
帮忙看一下这个程序怎么有问题啊?谢谢
/*********阶乘之和**********/
# include<stdio.h>
void func(int n);
int main(void)
{ int n;
long sum=0,i;
long m;
printf("请输入n的值\n");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
sum=sum+m ;
}
printf("1!+2!+3!+4!+......+n!=%1d\n",sum);
return 0;
}
void func(int n)
{
long i,m=1;
for(i=1;i<=n;i++)
{
m=m*i;
}
}有问题啊?谢谢!
回复列表 (共6个回复)
沙发
jdsbj [专家分:0] 发布于 2010-11-19 21:22:00
你的程序能运行出结果,编译没出错?
下面是我编写的,你看一下吧。
#include <stdio.h>
long func(int n)
{
long i,m=1;
for(i=1;i<=n;i++)
m=m*i;
return m;
}
main()
{
int n,i;
long sum=0;
printf("Please input n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d!+",i);
sum=sum+func(i);
}
printf("\b=%1d\n",sum);
}
板凳
josephkwok [专家分:530] 发布于 2010-11-21 21:18:00
唉,错漏百出啊...楼主自己先检查程序吧.
我不知道你的程序想干嘛.
m 没有初始化
func 从来没有调用过
3 楼
落花季节 [专家分:0] 发布于 2010-11-24 11:58:00
#include<iostream>
using namespace std;
int main()
{
int n,i;
long sum=0;
int func(int);
cout<<" please input n: ";
cin>>n;
for(i=1;i<=n;i++)
sum+=func(i);
cout<<"1!+2!+……+n!="<<sum<<endl;
return 0;
}
int func(int n)
{
int m,i;
m=1;
for(i=1;i<=n;i++)
m*=i;
return m;
}
4 楼
cfj469933485 [专家分:950] 发布于 2010-11-24 12:59:00
二楼说的不错。主函数m没有初始化,func函数没有调用
以下是我编的,仅供参考:
/*阶乘和
输入正整数n, 计算阶乘和: 1! + 2! + 3! + … + n!
int SumOfFac(int n); // SumOfFac(3) = 9, SumOfFac(5) = 153
*/
#include <stdio.h>
int SumOfFac(int n);
int Factorial(int n);
int main()
{
int k = 1, n;
printf("请输入正整数n: ");
scanf("%d", &n);
for(k; k <= n; k ++)
{
printf("%d! + ", k);
}
printf("\b\b= %d\n", SumOfFac(n));
return 0;
}
int SumOfFac(int n)
{
int k,sum = 0;
for(k = 1; k <= n; k ++)
{
sum += Factorial(k);
}
return sum;
}
int Factorial(int n)
{
if(n == 1 || n == 0) return 1;
return Factorial(n - 1) * n;
}
5 楼
mengxing0905 [专家分:0] 发布于 2010-11-25 09:43:00
func函数没有调用,而且m为局部变量....
6 楼
dfsafds [专家分:30] 发布于 2010-12-03 13:07:00
Practicality [url=http://www.mmopowerlevel.net]wow power leveling[/url] is also very important when choosing a gadget [url=http://www.mogxe.com/PowerLevel.php?gid=1]wow power leveling[/url] to give. You don’t want to spend a lot of money on [url=http://www.mmopowerlevel.net/buy.php]wow gold uk[/url] something only to find out that it is useless. Better to give something that the [url=http://www.mmopowerlevel.net/powerlist.php?fid=688]wow power leveling[/url] other person can use. You may not know it but you actually need a stun gun. In our world today where you are unsure of your safety, you need a stun gun to protect yourself. Why don’t you [url=http://www.mogxe.com]wow power leveling[/url] give it as a gift too? There is new stun master stun guns that you can buy as a gift. This stun gun is the best and it [url=http://www.mogxe.com/PowerLevel.php?gid=21]aion power level[/url] can protect the person you are giving this gift to.
Giving gadgets as gifts during the holidays is great. These [url=http://www.mogxe.com/PowerLevel.php?gid=21]aion powerleveling[/url] gadgets are useful and people like having them. If [url=http://www.mogxe.com/BuyGold.php?gid=1]buy wow gold[/url] you want to impress someone this is the best kind of gift that you can give to. No matter [url=http://www.mogxe.com/BuyGold.php?gid=21]aion kinah[/url] if that person is a girl or boy or whatever the age is, they would surely appreciate [url=http://www.mmopowerlevel.net/powerlist.php?fid=7422]aion power level[/url] having these electronic items as presents.
我来回复