Pancake numbers: Difference between revisions

Content added Content deleted
(Added Applesoft BASIC, BASIC256, Chipmunk Basic, Gambas, GW-BASIC, MSX Basic, PureBasic, QBasic, True BASIC and Yabasic)
m (Altered code to be more C++ idiomatic.)
Line 351: Line 351:
=={{header|C++}}==
=={{header|C++}}==
<syntaxhighlight lang="c++">
<syntaxhighlight lang="c++">

#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
Line 359: Line 360:
#include <iomanip>
#include <iomanip>


std::vector<int32_t> flip_stack(std::vector<int32_t> stack, int32_t index) {
std::vector<int32_t> flip_stack(std::vector<int32_t>& stack, const int32_t index) {
reverse(stack.begin(), stack.begin() + index);
reverse(stack.begin(), stack.begin() + index);
return stack;
return stack;
}
}


std::pair<std::vector<int32_t>, int32_t> pancake(int32_t number) {
std::pair<std::vector<int32_t>, int32_t> pancake(const int32_t number) {
std::vector<int32_t> initial_stack(number);
std::vector<int32_t> initial_stack(number);
std::iota(initial_stack.begin(), initial_stack.end(), 1);
std::iota(initial_stack.begin(), initial_stack.end(), 1);
Line 396: Line 397:


int main() {
int main() {
for ( int n = 1; n <= 9; ++n ) {
for ( int32_t n = 1; n <= 9; ++n ) {
std::pair<std::vector<int32_t>, int32_t> result = pancake(n);
std::pair<std::vector<int32_t>, int32_t> result = pancake(n);
std::cout << "pancake(" << n << ") = " << std::setw(2) << result.second << ". Example [";
std::cout << "pancake(" << n << ") = " << std::setw(2) << result.second << ". Example [";