<分区>
所以我让这段代码起作用了,因为幸运的是我记得函数原型(prototype)!我对 C++ 很陌生。我真的很好奇如何在某处使用空格将其变成一个实际的三角形。我承认我并没有不知疲倦地试图弄清楚这个问题。我只是在寻找它看起来不像图片,而是一个实际的三角形。非常感谢您的帮助。
//
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
void genPyrN(int rows)
{
if (rows < 0) return;
// save the last row here
std::vector<int> last(1, 1);
std::cout << last[0] << std::endl;
for (int i = 1; i <= rows; i++) {
// work on the next row
std::vector<int> thisRow;
thisRow.reserve(i+1);
thisRow.push_back(last.front()); // beginning of row
std::transform(last.begin(), last.end()-1, last.begin()+1, std::back_inserter(thisRow), std::plus<int>()); // middle of row
thisRow.push_back(last.back()); // end of row
for (int j = 0; j <= i; j++)
std::cout << thisRow[j] << " ";
std::cout << std::endl;
last.swap(thisRow);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
genPyrN(9);
cin.get();
cin.get();
return 0;
}