C#迴圈問題for do/while

使用for do/while迴圈計算

1+1/2+1/3....1/n n=67

1*1+2*2+3*3....+n*n n=34

一根繩子長5m每次對折 須折幾次 長度才會小於20cm

請問能說明講解如何編寫嗎?小弟出學C#

很多地方不懂

Update:

1/1 + 1/2 + 1/3 + .. + 1/{0} = {1}674.78935240737623

1*1 + 2*2 + 3*3 + .. + {0}*{0} = {1}3413685

a string of {0} cm in length needs to fold {1} times to be shorter than 20.ocm5005

由大大程式解出會變成這樣

這句須對折5005次嗎

a string of {0} cm in length needs to fold {1} times to be shorter than 20.ocm5005

Update 2:

Console.WriteLine我不會使用

所以我用output.text產生

會影響到答案嗎?

Update 3:

output.Text += "a string of"+ len+ "cm in length needs to fold"+strt +" times to be shorter than 20.ocm" + "\r\n";

第三題問題可以改成這樣?

1 Answer

Rating
  • 8 years ago
    Favorite Answer

    namespace ConsoleApplication1

    {

    class Program

    {

    static void Main(string[] args)

    {

    const int nd = 67;

    const int n2 = 34;

    const double len = 500.0;

    double sum;

    int sum2;

    int strt;

    for(sum = 0.0, strt=nd; strt > 0; --strt)sum += (1.0/strt);

    Console.WriteLine("1/1 + 1/2 + 1/3 + .. + 1/{0} = {1}",nd, sum);

    for(sum2 = 0, strt = n2; strt > 0; --strt) sum2 += (strt*strt);

    Console.WriteLine("1*1 + 2*2 + 3*3 + .. + {0}*{0} = {1}", n2, sum2);

    for(sum = len, strt = 0; sum >= 20.0; ++strt, sum /= 2.0);

    Console.WriteLine("a string of {0} cm in length needs to fold {1} times to be shorter than 20.ocm",

    len, strt);

    Console.Read();

    }

    }

    }

    2013-01-03 06:14:26 補充:

    Console.WriteLine我不會使用 - Console object is defined in Visual C# 2008

    所以我用output.text產生 會影響到答案嗎? (yes)

    2013-01-03 06:15:33 補充:

    output.Text += "a string of"+ len+ "cm in length needs to fold"+strt +" times to be shorter than 20.ocm" + "\\r\\n";

    第三題問題可以改成這樣? yes, as long as it works for you.

    I never used "output" object.

Still have questions? Get your answers by asking now.