输入一个正整数n 用3种方法分别计算下式的和 保留4位小数
e=1+1/1!+1/2!+1/3!+....+1/n!
1.使用一重循环,不使用自定义函数.
2.定义和调用函数fact(n)计算n的函数
3.使用嵌套循环
/
printf(", total).4f\ i <
for (j = i;= n;/
total += temp;= n;
double total = 0.h"
double total = 0:%:%;;= n;嵌套循环, temp = 1.0, total); j >.0; i <.0;
total = total + temp.0; i;;
/%d" j, &n); j--)
temp = temp / i++)
{
temp = temp /
}
return total;
double fact(int n)
{
int i.0;= 1;n"
}
int main(void)
{
int i; i++)
{
temp = 1, temp = 1; i <;
for (i = 1;调用函数
total = 0.4f\.0;嵌套循环
total = 0;
scanf(", fact(n));/
// i, n;调用函数;n".4f\一重循环
for (i = 1:%; i++)
{
temp = temp /一重循环;, j;
total = total + temp;
for (i = 1#include "
}
printf("
}
printf("n"
return 0
/
total);嵌套循环;调用函数; i++)
total += temp /.0;一重循环
&n).4f\= n.4f\ j--)
total = total + temp /
}
int main(void)
{
int i;%d"
/= j;
printf("
return 0
j;= n;n"= 1
n; i++)
{
temp = 1;n" i <.4f\.0; i++)
total = total + temp /
scanf("
for (i = 1;/
for (i = 1;
double total = 0.0;iostream>= i.0;
for (j = i;
temp = 1;
using namespace std;;
fact(n))
total); j >
/
double total = 0; i < i <;
printf("调用函数
total = 0;n":%:%:%;一重循环
for (i = 1;/.0;嵌套循环
total = 0;= n;
return total
printf(", total).4f\ i <
for (j = i;= n;/
total += temp;= n;
double total = 0.h"
double total = 0:%:%;;= n;嵌套循环, temp = 1.0, total); j >.0; i <.0;
total = total + temp.0; i;;
/%d" j, &n); j--)
temp = temp / i++)
{
temp = temp /
}
return total;
double fact(int n)
{
int i.0;= 1;n"
}
int main(void)
{
int i; i++)
{
temp = 1, temp = 1; i <;
for (i = 1;调用函数
total = 0.4f\.0;嵌套循环
total = 0;
scanf(", fact(n));/
// i, n;调用函数;n".4f\一重循环
for (i = 1:%; i++)
{
temp = temp /一重循环;, j;
total = total + temp;
for (i = 1#include "
}
printf("
}
printf("n"
return 0
/
total);嵌套循环;调用函数; i++)
total += temp /.0;一重循环
&n).4f\= n.4f\ j--)
total = total + temp /
}
int main(void)
{
int i;%d"
/= j;
printf("
return 0
j;= n;n"= 1
n; i++)
{
temp = 1;n" i <.4f\.0; i++)
total = total + temp /
scanf("
for (i = 1;/
for (i = 1;
double total = 0.0;iostream>= i.0;
for (j = i;
temp = 1;
using namespace std;;
fact(n))
total); j >
/
double total = 0; i < i <;
printf("调用函数
total = 0;n":%:%:%;一重循环
for (i = 1;/.0;嵌套循环
total = 0;= n;
return total
温馨提示:答案为网友推荐,仅供参考
第1个回答 2010-05-18
#include <iostream>
using namespace std;
double fact(int n)
{
int i;
double total = 0.0, temp = 1.0;
for (i = 1; i <= n; i++)
total = total + temp /= i;
return total;
}
int main(void)
{
int i, j, n;
double total = 0.0, temp = 1.0;
scanf("%d", &n);
//一重循环
for (i = 1; i <= n; i++)
total += temp /= i;
printf("一重循环:%.4f\n", total);
//调用函数
total = 0.0;
printf("调用函数:%.4f\n", fact(n));
//嵌套循环
total = 0.0;
for (i = 1; i <= n; i++)
{
temp = 1.0;
for (j = i; j >= 1; j--)
total = total + temp /= j;
printf("嵌套循环:%.4f\n", total);
return 0;
}
using namespace std;
double fact(int n)
{
int i;
double total = 0.0, temp = 1.0;
for (i = 1; i <= n; i++)
total = total + temp /= i;
return total;
}
int main(void)
{
int i, j, n;
double total = 0.0, temp = 1.0;
scanf("%d", &n);
//一重循环
for (i = 1; i <= n; i++)
total += temp /= i;
printf("一重循环:%.4f\n", total);
//调用函数
total = 0.0;
printf("调用函数:%.4f\n", fact(n));
//嵌套循环
total = 0.0;
for (i = 1; i <= n; i++)
{
temp = 1.0;
for (j = i; j >= 1; j--)
total = total + temp /= j;
printf("嵌套循环:%.4f\n", total);
return 0;
}
第2个回答 推荐于2017-09-10
#include "stdio.h"
double fact(int n)
{
int i;
double total = 0.0, temp = 1.0;
for (i = 1; i <= n; i++)
{
temp = temp / i;
total = total + temp;
}
return total;
}
int main(void)
{
int i, j, n;
double total = 0.0, temp = 1.0;
scanf("%d", &n);
//一重循环
for (i = 1; i <= n; i++)
{
temp = temp / i;
total += temp;
}
printf("一重循环:%.4f\n", total);
//调用函数
total = 0.0;
printf("调用函数:%.4f\n", fact(n));
//嵌套循环
total = 0.0;
for (i = 1; i <= n; i++)
{
temp = 1.0;
for (j = i; j >= 1; j--)
temp = temp / j;
total = total + temp;
}
printf("嵌套循环:%.4f\n", total);
return 0;
}本回答被提问者采纳
double fact(int n)
{
int i;
double total = 0.0, temp = 1.0;
for (i = 1; i <= n; i++)
{
temp = temp / i;
total = total + temp;
}
return total;
}
int main(void)
{
int i, j, n;
double total = 0.0, temp = 1.0;
scanf("%d", &n);
//一重循环
for (i = 1; i <= n; i++)
{
temp = temp / i;
total += temp;
}
printf("一重循环:%.4f\n", total);
//调用函数
total = 0.0;
printf("调用函数:%.4f\n", fact(n));
//嵌套循环
total = 0.0;
for (i = 1; i <= n; i++)
{
temp = 1.0;
for (j = i; j >= 1; j--)
temp = temp / j;
total = total + temp;
}
printf("嵌套循环:%.4f\n", total);
return 0;
}本回答被提问者采纳