史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   要怎麼用c++寫出這段運算式呢? (http://forum.slime.com.tw/thread166403.html)

lirere 2006-01-21 04:12 AM

要怎麼用c++寫出這段運算式呢?
 
1(1*n)+2[1*(n-1)]+.....+n(1*1)

怎麼用c++寫出這段運算式呢?
我寫了幾個都偵測到不少錯誤說= =
那個n是要可以讓我們自己輸入的.. :decfghi6:

mini 2006-01-21 09:20 AM

語法:

#include <cstdlib>
#include <iostream>

using namespace std;

int N;

long A(int n)
{
 return (N-n);
}

int main(int argc, char *argv[])
{
    int n;
    long sum=0;
   
    printf("請輸入階層數:");
    cin >> N;
    for(n=1; n<=N; n++){
      cout << n << "*[1*(N-" << n-1 << ")]";
      sum=sum+(n*A(n-1));
      if(n<N){
        cout << " + ";
      }else{
        cout << "=" << sum << '\n';
      }
    }
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

以上當然可以簡化

lirere 2006-01-21 04:28 PM

嗯嗯...我懂了^^

謝謝版大幫忙


所有時間均為台北時間。現在的時間是 09:48 AM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2024, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1