promotion image of download ymail app
Promoted

以C++寫出1+2+3+4+...+N=?

我寫不出來~\"~

請以

#include<stdio.h>

#include<stdlib.h>

寫就好

因為我們只交到這裡...

或者大大可以幫我解開這個錯誤

我的答案都是2293612

此寫法是參考麗山高中的考題解答...

感恩@@\"

#include<stdio.h>

#include<stdlib.h>

int main()

{

int a=1,n,s=0;

printf(\"請輸入一數n:\");

scanf(\"%d\",&n);

while(a<=n)

{

s=s+a;

a++;

}

printf(\"%d\",&s);

system(\"pause\");

return 0;

}

Update:

啊!真抱歉...

先感謝各位大大的回應

不過我忘記補充這個了!

以while(條件式)寫

像那個#include 跟 for我還不會= =""

真的很抱歉!!

5 Answers

Rating
  • 1 decade ago
    Favorite Answer

    #include <iostream.h>

    void main()

    {

    int sum,total=0;

    cout << "Key in a Max Value:" << endl;

    cin >> sum;

    for(int i=0;i<=sum;i++)

    {

    total=i+total;

    }

    cout << total << endl;

    }

    2006-04-05 14:03:29 補充:

    #include <iostream.h>void main(){int sum,total=0,i=0;cout << "Key in a Max Value:" << endl;cin >> sum;while(i<=sum){total=total+i;i++;}cout << total << endl;}

    • Commenter avatarLogin to reply the answers
  • 給Mars:...我最佳評價按錯人了= =""...真的很抱歉@@""...

    你才是最棒的@@"

    • Commenter avatarLogin to reply the answers
  • 青嶼
    Lv 4
    1 decade ago

    #include<stdio.h>

    void main(){

    int sum = 0;

    int n = 0;

    printf("請輸入一個數值N\n");

    scanf("%d",&n);

    for(int i=1;i<=n;i++){

    sum = sum + i;

    }

    printf("\n累加之後的結果\n");

    printf("%d",&sum);

    system("pause");

    }

    • Commenter avatarLogin to reply the answers
  • #include <stdio.h>

    int main()

    {

    int n;

    scanf("%d",&n);

    printf("%d",(1+n)*n/2);

    }

    It's just a math problem.

    2006-04-05 00:56:15 補充:

    #include 檔案在C裡面是必要的不會的話就不用管他我說過 這只是一個數學問題可以用常數時間解決的問題 何必用到while-loop來浪費CPU的時間?

    Source(s): me
    • Commenter avatarLogin to reply the answers
  • How do you think about the answers? You can sign in to vote the answer.
  • 1 decade ago

    #include<stdio.h>

    #include<stdlib.h>

    #include<iostream.h>

    int main(){

    int x=0,n,i;

    cout<<"Input:";

    cin>>n;

    for (i=1 ; i<=n ; i++){

    x+=i;

    }

    cout<<x<<endl;

    system("PAUSE");

    return 0;

    }

    有問題請提出

    2006-04-05 02:19:48 補充:

    #include<stdio.h>#include<stdlib.h>#include<iostream.h>int main(){int x=0,n;cout<<"Input:";cin>>n;while(n>=1){x+=n;n--;}cout<<x<<endl;system("PAUSE");return 0;}

    Source(s): 自己
    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.