Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Created November 18, 2010 08:25
Show Gist options
  • Save johnhmj/704773 to your computer and use it in GitHub Desktop.
Save johnhmj/704773 to your computer and use it in GitHub Desktop.
PTT C/C++ 2010.11.18
#include<iostream>
#include<cstdlib>
#include<vector>
using namespace std;
typedef vector<int> Vint;
int main(int argc, char* argv[])
{
//int i,sum=0,n;
//vector<int> avector;
//printf("請問要計算多少數字的總和");
//scanf("%d",&n);
int n = 0, sum = 0;
cout << "Input N: ", cin >> n;
Vint avtr;
for (int i = 0; i < n; i ++)
{
//printf("請輸入第%d個數字:",i+1);
//scanf("%d",&avector[i]);
int nbr = 0;
cout << "Input no." << (i + 1) << ": ";
cin >> nbr;
avtr.push_back(nbr);
}
// 迭代器 it 它是指標
for (Vint::iterator it = avtr.begin(); it != avtr.end(); it ++)
{
sum += *(it);
}
//printf("總和等於=%d\n",sum);
cout << "The sum = " << sum << endl;
system("PAUSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment