From 98b5fa54981ac564ef50b45b9acb76bd2c79da7c Mon Sep 17 00:00:00 2001 From: Pravallika11112345678 Date: Sat, 25 Oct 2025 21:15:55 +0530 Subject: [PATCH] updated... --- Hacktobercpp/count_digits.cpp | 29 +++-- Josephus Circular Linked List/main.cpp | 135 +++++++++------------ Tic-Tac-Toe/index.html | 105 +++++++++++----- Tic-Tac-Toe/style.css | 161 ++++++++++++++----------- 4 files changed, 239 insertions(+), 191 deletions(-) diff --git a/Hacktobercpp/count_digits.cpp b/Hacktobercpp/count_digits.cpp index b437db5..c23ef3e 100644 --- a/Hacktobercpp/count_digits.cpp +++ b/Hacktobercpp/count_digits.cpp @@ -1,9 +1,12 @@ -/*Given a number n. Count the number of digits in n which evenly divide n. Return an integer, total number of digits of n which divides n evenly. - -Note :- Evenly divides means whether n is divisible by a digit i.e. leaves a remainder 0 when divided.*/ -#include +#include using namespace std; +/* +Given a number N, count the number of digits in N +which evenly divide N. +Return the count of such digits. +*/ + class Solution { public: int evenlyDivides(int N) { @@ -11,11 +14,11 @@ class Solution { int temp = N; while (temp > 0) { - int last_digit = temp % 10; - temp = temp / 10; + int digit = temp % 10; // Get last digit + temp /= 10; // Remove last digit - // Check if the last digit is non-zero and divides N - if (last_digit != 0 && N % last_digit == 0) { + // Check if digit is non-zero and divides N + if (digit != 0 && N % digit == 0) { count++; } } @@ -24,16 +27,20 @@ class Solution { } }; -// Driver Code Starts. +// Driver code int main() { int t; + cout << "Enter number of test cases: "; cin >> t; while (t--) { int N; + cout << "Enter a number: "; cin >> N; - Solution ob; - cout << ob.evenlyDivides(N) << endl; + + Solution obj; + cout << "Number of digits evenly dividing " << N << " is: " + << obj.evenlyDivides(N) << endl; } return 0; diff --git a/Josephus Circular Linked List/main.cpp b/Josephus Circular Linked List/main.cpp index 80531f2..eb3f2bd 100644 --- a/Josephus Circular Linked List/main.cpp +++ b/Josephus Circular Linked List/main.cpp @@ -1,127 +1,108 @@ #include -#include -#include #include +#include using namespace std; +// Node structure for circular linked list struct Node { - string payload; + string name; Node* next; }; -/** -* constructing a new load with the parameter as a the payload -*/ -Node* newNode(string payload) { +// Function to create a new node +Node* createNode(const string& name) { Node* node = new Node(); - node->payload = payload; + node->name = name; node->next = nullptr; return node; } -/** -* loadGame takes an int and a string vector of names and loads a circular -* linked list (of n size), with the names from the vector -*/ -Node* loadGame(int n, vector names) { - if(n == 0) { - return nullptr; - } +// Function to load names into a circular linked list +Node* loadGame(int n, const vector& names) { + if (n == 0) return nullptr; + if (n != names.size()) throw runtime_error("Number of names does not match n"); - if(n != names.size()) { - throw runtime_error("Mismatch: n does not match number of names provided"); - } - Node* head = nullptr; Node* prev = nullptr; - string name; for (int i = 0; i < n; ++i) { - name = names.at(i); - Node* newNodePtr = newNode(name); - if (head == nullptr) { - head = newNodePtr; // initialize head specially - } else { - prev->next = newNodePtr; - } - prev = newNodePtr; + Node* node = createNode(names[i]); + if (!head) head = node; + else prev->next = node; + prev = node; } - prev->next = head; //make circular + prev->next = head; // make it circular return head; } -/** -* prints out the data of each node in the linked list -*/ -void print(Node* start) { // prints list +// Function to print circular linked list +void printList(Node* start) { + if (!start) return; Node* curr = start; - while (curr != nullptr) { - cout << curr->payload << endl; + do { + cout << curr->name << " "; curr = curr->next; - if (curr == start) { - break; // exit circular list - } - } + } while (curr != start); + cout << endl; } -/** -* Runs the Josephus algorithm on a circular linked list. This algorithm simulates -* a game where every k-th person is eliminated until only one person remains. -* The function takes two parameters, start, which is a node*, pointing to the start, and an integer -* which is how many nodes to iterate past -*/ -Node* runGame(Node* start, int k) { // josephus w circular list, k = num skips - if(start == nullptr) { - return nullptr; - } +// Josephus game function +Node* runGame(Node* start, int k) { + if (!start) return nullptr; + if (k <= 0) throw runtime_error("k must be positive"); - if(k <= 0) { - throw runtime_error("k must be positive"); - } - Node* curr = start; - Node* prev = curr; - while (curr->next != curr) { // exit condition, last person standing - for (int i = 0; i < k; ++i) { // find kth node + Node* prev = nullptr; + + while (curr->next != curr) { // more than one person left + // skip k-1 nodes + for (int i = 0; i < k - 1; ++i) { prev = curr; curr = curr->next; } + // eliminate curr + cout << "Eliminated: " << curr->name << endl; prev->next = curr->next; Node* temp = curr; curr = curr->next; delete temp; } - return curr; // last person standing + return curr; // last person remaining } -/* Driver program to test above functions */ int main() { - int n=1, k=1, max; // n = num names; k = num skips (minus 1) - string name; - vector names; + int n, k; + cout << "Enter number of players: "; + cin >> n; + cout << "Enter step count (k): "; + cin >> k; - // get inputs and check if cin is in a valid state - cin >> n >> k; - if(!cin) { throw runtime_error("Faulty input"); } - if(n <= 0 || k < 0) { throw runtime_error("n must be positive, k must be non-negative"); } + if (n <= 0 || k <= 0) { + cout << "Invalid input: n and k must be positive integers." << endl; + return 1; + } - while (cin >> name && name != ".") { names.push_back(name); } // EOF or . ends input + cout << "Enter names of players (enter '.' to finish):" << endl; + vector names; + string name; + for (int i = 0; i < n; ++i) { + cin >> name; + names.push_back(name); + } - // initialize and run game Node* startPerson = loadGame(n, names); + + cout << "\nInitial Circle of Players:" << endl; + printList(startPerson); cout << endl; - print(startPerson); - cout << endl; - Node* lastPerson = runGame(startPerson, k); - if (lastPerson != nullptr) { - cout << lastPerson->payload << " wins!" << endl; - } else { - cout << "error: null game" << endl; + Node* winner = runGame(startPerson, k); + + if (winner) { + cout << "\nWinner is: " << winner->name << "!" << endl; + delete winner; // clean up last node } - - delete lastPerson; // delete the last node remaining in the heap return 0; } diff --git a/Tic-Tac-Toe/index.html b/Tic-Tac-Toe/index.html index 904fffc..1ba9536 100644 --- a/Tic-Tac-Toe/index.html +++ b/Tic-Tac-Toe/index.html @@ -1,33 +1,78 @@ - - - - Tic-Tac-Toe Game - - - -
-

Winner

- -
-
-

Tic Tac Toe

-
-
- - - - - - - - - -
+ + + + Tic-Tac-Toe | Hacktoberfest Project + + + + + +
+

Winner

+ +
+ +
+

Tic Tac Toe

+ +
+
+ + + + + + + + + +
- -
- - - \ No newline at end of file +
+ + +
+ + + + + diff --git a/Tic-Tac-Toe/style.css b/Tic-Tac-Toe/style.css index 2282f43..37a5208 100644 --- a/Tic-Tac-Toe/style.css +++ b/Tic-Tac-Toe/style.css @@ -1,74 +1,89 @@ +/* Reset default margins and paddings */ * { - margin: 0; - padding: 0; - } - - body { - background-color: #548687; - text-align: center; - } - - .container { - height: 70vh; - display: flex; - - justify-content: center; - align-items: center; - } - - .game { - height: 60vmin; - width: 60vmin; - display: flex; - flex-wrap: wrap; - justify-content: center; - align-items: center; - gap: 1.5vmin; - } - - .box { - height: 18vmin; - width: 18vmin; - border-radius: 1rem; - border: none; - box-shadow: 0 0 1rem rgba(0, 0, 0, 0.3); - font-size: 8vmin; - color: #b0413e; - background-color: #ffffc7; - } - - #reset-btn { - padding: 1rem; - font-size: 1.25rem; - background-color: #191913; - color: #fff; - border-radius: 1rem; - border: none; - } - - #new-btn { - padding: 1rem; - font-size: 1.25rem; - background-color: #191913; - color: #fff; - border-radius: 1rem; - border: none; - } - - #msg { - color: #ffffc7; - font-size: 5vmin; - } - - .msg-container { - height: 100vmin; - display: flex; - justify-content: center; - align-items: center; - flex-direction: column; - gap: 4rem; - } - - .hide { - display: none; - } \ No newline at end of file + margin: 0; + padding: 0; + box-sizing: border-box; +} + +/* Body styling */ +body { + background-color: #548687; + text-align: center; + font-family: Arial, sans-serif; +} + +/* Container to center the game */ +.container { + height: 70vh; + display: flex; + justify-content: center; + align-items: center; +} + +/* Game grid */ +.game { + height: 60vmin; + width: 60vmin; + display: flex; + flex-wrap: wrap; + justify-content: center; + align-items: center; + gap: 1.5vmin; +} + +/* Individual box styling */ +.box { + height: 18vmin; + width: 18vmin; + border-radius: 1rem; + border: none; + box-shadow: 0 0 1rem rgba(0, 0, 0, 0.3); + font-size: 8vmin; + color: #b0413e; + background-color: #ffffc7; + cursor: pointer; + transition: transform 0.2s ease; +} + +.box:hover { + transform: scale(1.05); +} + +/* Reset and new game buttons */ +#reset-btn, +#new-btn { + padding: 1rem 2rem; + font-size: 1.25rem; + background-color: #191913; + color: #fff; + border-radius: 1rem; + border: none; + cursor: pointer; + transition: background-color 0.2s ease; +} + +#reset-btn:hover, +#new-btn:hover { + background-color: #3a3a3a; +} + +/* Winner / message styling */ +#msg { + color: #ffffc7; + font-size: 5vmin; +} + +/* Message container */ +.msg-container { + height: 100vmin; + display: flex; + justify-content: center; + align-items: center; + flex-direction: column; + gap: 4rem; +} + +/* Hide container by default */ +.hide { + display: none; +}