public static ulong Factorial(ulong m) { ulong ret = 1; for (ulong i = 1; i <= m; i++) ret *= i; return ret; } public static ulong FactorialSum(ulong n) { ulong ret = 0; for (int i = 1; i <= n; i++) ret += Factorial(i); return ret; } public static void Main() { Console.Write("请输入n:"); ulong n; if (ulong.TryParse(Console.ReadLine(), out n)) { Console.WriteLine("{1}", FactorialSum(n)); } else { Console.WriteLine("输入的数字出错!"); } }
protected int JieCheng(int num)//求某一个数的阶乘 { int result = 1; if (num < 1) { return result; } else { for (int i = 1; i <= num; i++) { result = result * i; } return result; } }
protected int sum()//计算出1-10阶乘的总和 { int result = 1; for (int i = 1; i <= 10; i++) { int temp = JieCheng(i); result = result + temp; } return result; }
oop1: //编写程序,求函数x <0时y=-1;x=0时y=0;x>0时y=1 的值 int x, y = 0; x = int.Parse(Console.ReadLine()); TimeSpan t1 = new TimeSpan(DateTime.Now.Ticks);//获取当前时间的刻度 for (int i = 0; i < 1000000; i++) y = x == 0 ? 0 : x / Math.Abs(x); TimeSpan t2 = new TimeSpan(DateTime.Now.Ticks); TimeSpan ts = t2.Subtract(t1).Duration();//时间差的绝对值 Console.WriteLine("A算法:" + y.ToString() + "执行时间为:" + ts.TotalMilliseconds); t1 = new TimeSpan(DateTime.Now.Ticks);//获取当前时间的刻度 for (int i = 0; i < 1000000; i++) y = x < 0 ? -1 : x == 0 ? 0 : 1; t2 = new TimeSpan(DateTime.Now.Ticks); ts = t2.Subtract(t1).Duration();//时间差的绝对值 Console.WriteLine("B算法:" + y.ToString() + "执行时间为:" + ts.TotalMilliseconds); goto loop1;经测试。得出答案: y = x == 0 ? 0 : x / Math.Abs(x);比 y = x < 0 ? -1 : x == 0 ? 0 : 1;在非0值时,运算要慢。其实就一行代码,但是看到了三篇这样的贴子
public static ulong Factorial(ulong m)
{
ulong ret = 1;
for (ulong i = 1; i <= m; i++) ret *= i;
return ret;
}
public static ulong FactorialSum(ulong n)
{
ulong ret = 0;
for (int i = 1; i <= n; i++) ret += Factorial(i);
return ret;
}
public static void Main()
{
Console.Write("请输入n:");
ulong n;
if (ulong.TryParse(Console.ReadLine(), out n))
{
Console.WriteLine("{1}", FactorialSum(n));
}
else
{
Console.WriteLine("输入的数字出错!");
}
}
protected int JieCheng(int num)//求某一个数的阶乘
{
int result = 1;
if (num < 1)
{
return result;
}
else
{
for (int i = 1; i <= num; i++)
{
result = result * i;
}
return result;
}
}
protected int sum()//计算出1-10阶乘的总和
{
int result = 1;
for (int i = 1; i <= 10; i++)
{
int temp = JieCheng(i);
result = result + temp;
}
return result;
}
//编写程序,求函数x <0时y=-1;x=0时y=0;x>0时y=1 的值
int x, y = 0;
x = int.Parse(Console.ReadLine());
TimeSpan t1 = new TimeSpan(DateTime.Now.Ticks);//获取当前时间的刻度
for (int i = 0; i < 1000000; i++)
y = x == 0 ? 0 : x / Math.Abs(x);
TimeSpan t2 = new TimeSpan(DateTime.Now.Ticks);
TimeSpan ts = t2.Subtract(t1).Duration();//时间差的绝对值
Console.WriteLine("A算法:" + y.ToString() + "执行时间为:" + ts.TotalMilliseconds); t1 = new TimeSpan(DateTime.Now.Ticks);//获取当前时间的刻度
for (int i = 0; i < 1000000; i++)
y = x < 0 ? -1 : x == 0 ? 0 : 1;
t2 = new TimeSpan(DateTime.Now.Ticks);
ts = t2.Subtract(t1).Duration();//时间差的绝对值
Console.WriteLine("B算法:" + y.ToString() + "执行时间为:" + ts.TotalMilliseconds);
goto loop1;经测试。得出答案:
y = x == 0 ? 0 : x / Math.Abs(x);比
y = x < 0 ? -1 : x == 0 ? 0 : 1;在非0值时,运算要慢。其实就一行代码,但是看到了三篇这样的贴子