在C++创建动态数组


计算首行两数相加

#include <iostream>11
#include <vector>
using namespace std;
int main()
{
	vector<int> ivec;
	int num;
	while (cin >> num)
	{
		ivec.push_back(num);
	}
	int count = ivec.size();
	for (int first = 0, last = count - 1; first <= last; first++, last--)
	{
		if (first == last)
		{
			cout << "中间数据为:" << ivec[last] << endl;
		}
		else
		cout << ivec[first] + ivec[last] << endl;
	}
	return 0;
}

  运行结果:

 


可以使用vector可以创造动态数组(如上),也可以使用

int n;
cin>>n;
int *array=new int [n];

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM