想用C#寫出一些語法希望有人可以幫忙

我想問如何使用c#寫出 一級數s=3+6+9+...,請問加到第幾項,其和剛好超過1000。

2 Answers

Rating
  • 1 decade ago
    Favorite Answer

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ConsoleApplication3

    {

    class Program

    {

    static void Main(string[] args)

    {

    int N = 1000; // change me, if you want!!!

    int i;

    double d = Math.Sqrt((8.0f*N+3.0f)/3.0f);

    d = (d - 1.0f) / 2.0f;

    i = 1+(int)d;

    Console.WriteLine("Sum[1.."+ i + "] > " + N + ", when item number reaches " + i);

    /*

    int j;

    for (i = 0, j = 0; i <= N; ++j)

    Console.WriteLine("a[" + j + "]=" + 3*j + "sum[1.." + j+"]=" + (i+=3*j));

    */

    Console.ReadLine();

    }

    }

    }

  • 1 decade ago

    用 for , 迴圈中用if 判斷總和超過1000則跳出迴圈

Still have questions? Get your answers by asking now.