Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- #include <conio.h>
- #include <string>
- #include <ctime>
- class node {
- public:
- node(int v, node* p = NULL) {
- this->value = v;
- this->prev = p;
- }
- int value;
- node* prev;
- node* firstChild = NULL;
- node* secondChild = NULL;
- node* thirdChild = NULL;
- node* fourthChild = NULL;
- void createNodes();
- };
- class graph {
- public:
- graph(node* s) {
- this->start = s;
- }
- node* start;
- node* search(int);
- };
- void node::createNodes() {
- //create child nodes
- if(this->value * 3 >= 0) this->firstChild = new node(this->value * 3, this);
- if(this->value * 2 - 323 >= 0) this->secondChild = new node(this->value * 2 - 323, this);
- if(this->value + 27 >= 0) this->thirdChild = new node(this->value + 27, this);
- if(this->value - 13 >= 0) this->fourthChild = new node(this->value - 13, this);
- }
- node* graph::search(int search) {
- std::vector<node*> layer, beta;
- //add start node to vector
- beta.push_back(start);
- while(true) {
- //add previous child nodes to the list of nodes generated childs for
- layer = beta;
- //clear child node vector
- beta.clear();
- //parse every node in vector
- for each(node* n in layer) {
- //create child nodes for each node
- n->createNodes();
- //check each child node if it equals the searched number
- if(n->firstChild != NULL) {
- if(n->firstChild->value == search) return n->firstChild;
- beta.push_back(n->firstChild);
- }
- if(n->secondChild != NULL) {
- if(n->secondChild->value == search) return n->secondChild;
- beta.push_back(n->secondChild);
- }
- if(n->thirdChild != NULL) {
- if(n->thirdChild->value == search) return n->thirdChild;
- beta.push_back(n->thirdChild);
- }
- if(n->fourthChild != NULL) {
- if(n->fourthChild->value == search) return n->fourthChild;
- beta.push_back(n->fourthChild);
- }
- }
- }
- }
- int main() {
- setlocale(LC_ALL, "German");
- int to_find = 0;
- node* found = NULL;
- std::string opChain = "";
- std::cout << "Dieses Programm sucht eine Operationskette von 1 zu einer bestimmten, positiven Zahl." << std::endl << "Dabei dürfen nur die folgenden Operationen verwendet werden:" << std::endl;
- std::cout << "- Multiplikation mit 3" << std::endl;
- std::cout << "- Multiplikation mit 2 und anschließende Subtraktion von 323" << std::endl;
- std::cout << "- Addition von 27" << std::endl;
- std::cout << "- Subtraktion von 13" << std::endl;
- std::cout << std::endl << "Geben Sie nun eine Zahl ein, zu der die kürzeste Operationskette von 1 aus gefunden werden soll: ";
- while(!(std::cin >> to_find) || to_find < 0) {
- std::cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- std::cout << "Ihre Eingabe war fehlerhaft!" << std::endl << "Bitte wiederholen Sie Ihre Eingabe: ";
- }
- std::cout << "Berechne...";
- clock_t begin = clock();
- if(to_find != 1) {
- graph* g = new graph(new node(1));
- found = g->search(to_find);
- do {
- if(found->value == found->prev->value * 3) {
- opChain += '1';
- }
- else if(found->value == found->prev->value * 2 - 323) {
- opChain += '2';
- }
- else if(found->value == found->prev->value + 27) {
- opChain += '3';
- }
- else if(found->value == found->prev->value - 13) {
- opChain += '4';
- }
- found = found->prev;
- } while(found->prev != NULL);
- }
- clock_t end = clock();
- double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
- std::cout << "Fertig!" << std::endl << "Benötigte Zeit: " << elapsed_secs << "s" << std::endl << std::endl << "Kürzester Weg von 1 zu " << to_find << " (" << opChain.length() << " Schritte):" << std::endl;
- int n = 1;
- for(int i = opChain.length(); i >= 0; i--) {
- switch(opChain[i]) {
- case '1':
- std::cout << ">>Multiplikation mit 3: " << n << " * 3 = " << n * 3 << std::endl;
- n *= 3;
- break;
- case '2':
- std::cout << ">>Multiplikation mit 2 und anschließende Subtraktion von 323: " << n << " * 2 - 323 = " << n * 2 - 323 << std::endl;
- n = n * 2 - 323;
- break;
- case '3':
- std::cout << ">>Addition von 27: " << n << " + 27 = " << n + 27 << std::endl;
- n += 27;
- break;
- case '4':
- std::cout << ">>Subtraktion von 13: " << n << " - 13 = " << n - 13 << std::endl;
- n -= 13;
- break;
- }
- }
- std::cout << std::endl << "Drücken Sie eine beliebige Taste zum Beenden...";
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment