C#语言编写1+2+3...+100的结果

来源:百度知道 编辑:UC知道 时间:2024/05/30 22:52:30
各位帮帮忙啊``我根本没接触过C#..这些人居然考我这些...

int total =0;
for(int i=1;i<=100;i++)
{
total+=i;
}
Console.WriteLine(total);
应该是输出和

二楼都不错,可以去掉循环。如
Console.wirte((1+100)*55);
有时当我们将数学与程序结合起来,会很好,下面这种方式可以计算任意数的累加。
int max;//用户输入的任意整数
if(max%2==0){ Console.write((1+max)*max/2);
}
else{
Console.write((1+max)*(max+1)/2-max);
}

这个算的最快,不信你试试
int total = 0;
for(int i = 1;i <= 50;i++)
{
total = total + i + (100-i);
}
Console.WriteLine(total + 50);

他们给都要循环100次我这个只要50次。少一半的时间差啊。

有很多方法!
不知道你是学什么的?
c学过吗?

2,3,6楼都可以得到你要的答案
一般都用2楼的方法
估计你没有接触过C#的吧
知道C#的这个就没有问题

using System;
using System.Collections.Generic;
using System.Text;

namespace baiduzhidaototal73
{
class Program
{
static void Main(string[] args)
{
int total = 1, i = 2;
Total:
total