﻿ 算法入门 Algorithms Basic - 鸿网互联

# 算法入门 Algorithms Basic

Thisexampleimplementsseveralbasicalgorithms. 无 #includestdio.hint steps = 0;void move(int n, char start, char goal){printf("Move pile %d from %c to %c\n", n, start, goal);steps++;}int movePiles(int n, char start, char temp, char goal){if (
This example implements several basic algorithms. <无>
```#include<stdio.h>

int steps = 0;

void move(int n, char start, char goal)
{
printf("Move pile %d from %c to %c\n", n, start, goal);
steps++;
}

int movePiles(int n, char start, char temp, char goal)
{
if (n == 0) return steps;
movePiles(n - 1, start, goal, temp);
move(n, start, goal);
movePiles(n - 1, temp, start, goal);
}

void runMove()
{
printf("Input number of piles: ");
int piles;
scanf_s("%d", &piles);
movePiles(piles, 'A', 'B', 'C');
printf("Total number of steps %d\n", steps);
}```
```#pragma once
#include <vector>
#include<stdlib.h>
#include<iostream>

using namespace std;

template<typename T>
T honrnersRule(vector<T> coefList, vector<int> powerList, T variable)
{
T sum = 0;
for (int index = coefList.size() - 1; index >= 0; index--)
{
sum = coefList.at(index) + variable * sum;
}//end loop

return sum;
};//end function```
```#pragma once
#include<stdlib.h>
#include<stdio.h>

template<typename E>
void swap(E A[], int i, int j)
{
E temp = A[i];
A[i] = A[j];
A[j] = temp;
}

int random(int n)
{
return rand() % n;
}

//Get ONE OF the possible permutations of a given serie:
template<typename E>
void permute(E A[], int n)
{
for (int i = n; i > 0; i--)
swap(A, i - 1, random(i));
}```

<