c#求阶乘累加和方法!急急急
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
2个回答
2015-03-31 · 知道合伙人软件行家
关注
展开全部
递归方法:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(fun(6).ToString());
Console.ReadKey();
}
public static double fun(int n)
{
if (n == 1)
return 1;
else
return fun(n - 1) * n;
}
}
}
非递归方法:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(fun(6).ToString());
Console.ReadKey();
}
public static double fun(int n)
{
double S = 1;
for (int i = 0; i < n; i++)
{
S *= (i+1);
}
return S;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(fun(6).ToString());
Console.ReadKey();
}
public static double fun(int n)
{
if (n == 1)
return 1;
else
return fun(n - 1) * n;
}
}
}
非递归方法:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(fun(6).ToString());
Console.ReadKey();
}
public static double fun(int n)
{
double S = 1;
for (int i = 0; i < n; i++)
{
S *= (i+1);
}
return S;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在视图界面双击【确定】进入后台代码界面,写:
static int jiecheng(int num)
{
int j = 1;
for (int i = 1; i <= num; i++)
{
j = j * i;
}
return j;
}
protected void Button1_Click(object sender, EventArgs e)
{
int num = int.Parse(TextBox1.Text.Trim());
int j = jiecheng(num);
int sum = 0;
string str = "0!";
for (int i = 1; i <= num; i++)
{
str = str + "+" + string.Format("{0}!", i);
}
for (int i = 1; i <= num; i++)
{
sum = sum + jiecheng(i);
}
this.Label1.Text = str + "=" + sum.ToString();
//Response.Write(str + "=" + sum.ToString());
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询