Dados tres enteros A , B y L , la tarea es imprimir el conjunto cantor ternario desde el rango [A, B] hasta los niveles L.
Conjunto de Cantor ternario: Un conjunto de Cantor ternario es un conjunto que se construye eliminando la parte media de un segmento de línea cuando se divide en 3 partes y repitiendo este proceso con los segmentos restantes más cortos. A continuación se muestra una ilustración de un conjunto de cantor.
Ejemplos:
Entrada: A = 0, B = 1, L = 2
Salida:
Nivel 0: [0,000000] — [1,000000]
Nivel 1: [0,000000] — [0,333333] [0,666667] — [1,000000]
Nivel 2: [0,000000] — [ 0.111111] [0.222222] — [0.333333] [0.666667] — [0.777778] [0.888889] — [1.000000]
Explicación: Para el rango dado [0, 1], en el nivel 1, se divide en tres partes ([0, 0.33 ], [0,33, 0,67], [0,67, 1]). De las tres partes, se ignora la parte del medio. Este proceso se continúa para cada parte en las ejecuciones posteriores.
Entrada: A = 0, B = 9, L = 3
Salida:
Nivel_0: [0.000000] — [9.000000]
Nivel_1: [0.000000] — [3.000000] [6.000000] — [9.000000]
Nivel_2: [0.000000] — [1.000000] [2.000000] — [3.000000] [6.000000] — [7.000000] [8.000000] — [9.000000]
Nivel_3: [0.000000] — [0.333333] [0.6666602 [0] —0. [2,333333] [2,666667] — [3,000000] [6,000000] — [6,333333] [6,666667] — [7,000000] [8,000000] — [8,333333] [8,666667] — [9,000000]
Acercarse:
- Cree una estructura de datos de lista enlazada para cada Node del Conjunto, que tenga el valor inicial, el valor final y un puntero al siguiente Node.
- Inicialice la lista con el valor inicial y final proporcionado como entrada.
- Para el siguiente nivel:
- Cree un nuevo Node donde la diferencia entre los valores inicial y final sea la inicial, es decir, el valor inicial sea menor que el valor final inicial.
- Además, modifique el Node original, de modo que el valor final sea más del valor inicial inicial.
- Coloque el puntero en el nuevo Node después del original según corresponda.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to find the cantor set // for n levels and // for a given start_num and end_num #include <bits/stdc++.h> using namespace std; // The Linked List Structure for the Cantor Set typedef struct cantor { double start, end; struct cantor* next; } Cantor; // Function to initialize the Cantor Set List Cantor* startList(Cantor* head, double start_num, double end_num) { if (head == NULL) { head = new Cantor; head->start = start_num; head->end = end_num; head->next = NULL; } return head; } // Function to propagate the list // by adding new nodes for the next levels Cantor* propagate(Cantor* head) { Cantor* temp = head; if (temp != NULL) { Cantor* newNode = new Cantor; double diff = (((temp->end) - (temp->start)) / 3); // Modifying the start and end values // for the next level newNode->end = temp->end; temp->end = ((temp->start) + diff); newNode->start = (newNode->end) - diff; // Changing the pointers // to the next node newNode->next = temp->next; temp->next = newNode; // Recursively call the function // to generate the Cantor Set // for the entire level propagate(temp->next->next); } return head; } // Function to print a level of the Set void print(Cantor* temp) { while (temp != NULL) { printf("[%lf] -- [%lf]\t", temp->start, temp->end); temp = temp->next; } cout << endl; } // Function to build and display // the Cantor Set for each level void buildCantorSet(int A, int B, int L) { Cantor* head = NULL; head = startList(head, A, B); for (int i = 0; i < L; i++) { cout <<"Level_"<< i<<" : "; print(head); propagate(head); } cout <<"Level_"<< L<<" : "; print(head); } // Driver code int main() { int A = 0; int B = 9; int L = 2; buildCantorSet(A, B, L); return 0; } // This code is contributed by shivanisingh
C
// C implementation to find the cantor set // for n levels and // for a given start_num and end_num #include <stdio.h> #include <stdlib.h> #include <string.h> // The Linked List Structure for the Cantor Set typedef struct cantor { double start, end; struct cantor* next; } Cantor; // Function to initialize the Cantor Set List Cantor* startList(Cantor* head, double start_num, double end_num) { if (head == NULL) { head = (Cantor*)malloc(sizeof(Cantor)); head->start = start_num; head->end = end_num; head->next = NULL; } return head; } // Function to propagate the list // by adding new nodes for the next levels Cantor* propagate(Cantor* head) { Cantor* temp = head; if (temp != NULL) { Cantor* newNode = (Cantor*)malloc(sizeof(Cantor)); double diff = (((temp->end) - (temp->start)) / 3); // Modifying the start and end values // for the next level newNode->end = temp->end; temp->end = ((temp->start) + diff); newNode->start = (newNode->end) - diff; // Changing the pointers // to the next node newNode->next = temp->next; temp->next = newNode; // Recursively call the function // to generate the Cantor Set // for the entire level propagate(temp->next->next); } return head; } // Function to print a level of the Set void print(Cantor* temp) { while (temp != NULL) { printf("[%lf] -- [%lf]\t", temp->start, temp->end); temp = temp->next; } printf("\n"); } // Function to build and display // the Cantor Set for each level void buildCantorSet(int A, int B, int L) { Cantor* head = NULL; head = startList(head, A, B); for (int i = 0; i < L; i++) { printf("Level_%d : ", i); print(head); propagate(head); } printf("Level_%d : ", L); print(head); } // Driver code int main() { int A = 0; int B = 9; int L = 2; buildCantorSet(A, B, L); return 0; }
Java
// Java implementation to find the cantor set // for n levels and // for a given start_num and end_num class GFG { // The Linked List Structure for the Cantor Set static class Cantor { double start, end; Cantor next; }; static Cantor Cantor; // Function to initialize the Cantor Set List static Cantor startList(Cantor head, double start_num, double end_num) { if (head == null) { head = new Cantor(); head.start = start_num; head.end = end_num; head.next = null; } return head; } // Function to propagate the list // by adding new nodes for the next levels static Cantor propagate(Cantor head) { Cantor temp = head; if (temp != null) { Cantor newNode = new Cantor(); double diff = (((temp.end) - (temp.start)) / 3); // Modifying the start and end values // for the next level newNode.end = temp.end; temp.end = ((temp.start) + diff); newNode.start = (newNode.end) - diff; // Changing the pointers // to the next node newNode.next = temp.next; temp.next = newNode; // Recursively call the function // to generate the Cantor Set // for the entire level propagate(temp.next.next); } return head; } // Function to print a level of the Set static void print(Cantor temp) { while (temp != null) { System.out.printf("[%f] -- [%f]", temp.start, temp.end); temp = temp.next; } System.out.printf("\n"); } // Function to build and display // the Cantor Set for each level static void buildCantorSet(int A, int B, int L) { Cantor head = null; head = startList(head, A, B); for (int i = 0; i < L; i++) { System.out.printf("Level_%d : ", i); print(head); propagate(head); } System.out.printf("Level_%d : ", L); print(head); } // Driver code public static void main(String[] args) { int A = 0; int B = 9; int L = 2; buildCantorSet(A, B, L); } } // This code is contributed by Rajput-Ji
C#
// C# implementation to find the cantor set // for n levels and // for a given start_num and end_num using System; class GFG { // The Linked List Structure for the Cantor Set class Cantor { public double start, end; public Cantor next; }; static Cantor cantor; // Function to initialize the Cantor Set List static Cantor startList(Cantor head, double start_num, double end_num) { if (head == null) { head = new Cantor(); head.start = start_num; head.end = end_num; head.next = null; } return head; } // Function to propagate the list // by adding new nodes for the next levels static Cantor propagate(Cantor head) { Cantor temp = head; if (temp != null) { Cantor newNode = new Cantor(); double diff = (((temp.end) - (temp.start)) / 3); // Modifying the start and end values // for the next level newNode.end = temp.end; temp.end = ((temp.start) + diff); newNode.start = (newNode.end) - diff; // Changing the pointers // to the next node newNode.next = temp.next; temp.next = newNode; // Recursively call the function // to generate the Cantor Set // for the entire level propagate(temp.next.next); } return head; } // Function to print a level of the Set static void print(Cantor temp) { while (temp != null) { Console.Write("[{0:F6}] -- [{1:F6}]", temp.start, temp.end); temp = temp.next; } Console.Write("\n"); } // Function to build and display // the Cantor Set for each level static void buildCantorSet(int A, int B, int L) { Cantor head = null; head = startList(head, A, B); for (int i = 0; i < L; i++) { Console.Write("Level_{0} : ", i); print(head); propagate(head); } Console.Write("Level_{0} : ", L); print(head); } // Driver code public static void Main(String[] args) { int A = 0; int B = 9; int L = 2; buildCantorSet(A, B, L); } } // This code is contributed by Rajput-Ji
Javascript
<script> // Javascript implementation to find the cantor set // for n levels and // for a given start_num and end_num // The Linked List Structure for the Cantor Set class Cantor { constructor() { this.start = 0; this.end = 0; this.next = null; } }; var cantor = null; // Function to initialize the Cantor Set List function startList(head, start_num, end_num) { if (head == null) { head = new Cantor(); head.start = start_num; head.end = end_num; head.next = null; } return head; } // Function to propagate the list // by adding new nodes for the next levels function propagate(head) { var temp = head; if (temp != null) { var newNode = new Cantor(); var diff = (((temp.end) - (temp.start)) / 3); // Modifying the start and end values // for the next level newNode.end = temp.end; temp.end = ((temp.start) + diff); newNode.start = (newNode.end) - diff; // Changing the pointers // to the next node newNode.next = temp.next; temp.next = newNode; // Recursively call the function // to generate the Cantor Set // for the entire level propagate(temp.next.next); } return head; } // Function to print a level of the Set function print(temp) { while (temp != null) { document.write("["+temp.start.toFixed(6)+"] -- ["+ temp.end.toFixed(6)+"] "); temp = temp.next; } document.write("<br>"); } // Function to build and display // the Cantor Set for each level function buildCantorSet(A, B, L) { var head = null; head = startList(head, A, B); for (var i = 0; i < L; i++) { document.write("Level_"+ i +" : "); print(head); propagate(head); } document.write("Level_"+ L +" : "); print(head); } // Driver code var A = 0; var B = 9; var L = 2; buildCantorSet(A, B, L); </script>
Nivel_0 : [0.000000] — [9.000000]
Nivel_1 : [0.000000] — [3.000000] [6.000000] — [9.000000]
Nivel_2 : [0.000000] — [1.000000] [2.000000] — [3.000000]0 [0]0 —0.0 [8.000000] — [9.000000]
Referencias: Conjunto de Cantor Wikipedia
Artículo relacionado: N-ésimo término del conjunto de números racionales de George Cantor
Publicación traducida automáticamente
Artículo escrito por the_alphaEye y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA