0% found this document useful (0 votes)
15 views

ii

uu

Uploaded by

23020676
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

ii

uu

Uploaded by

23020676
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Bài 7.

#include <iostream>

#include <fstream>

#include <vector>

#include <algorithm>

using namespace std;

int main()

string filename;

cin >> filename;

ifstream file(filename);

if (!file.is_open())

cout << "Mission failed";

int n;

file >> n;

vector <int> a(n);

for (int i = 0; i < n; i++)

file >> a[i];

file.close();

sort(a.begin(), a.end());

int result;

if (n % 2 == 0)

result = (a[n/2-1] + a[n/2+1])/2;

else result = a[n/2];

cout << result;

Bài 6. string combineWordsFromFile(const char fileName[])


{

ifstream file(fileName);

if (!file.is_open()) {

cout << "Mission failed";

vector<string> a;

string word;

while (file >> word)

a.push_back(word);

file.close();

sort(a.begin(), a.end(), greater<string>());

string combined;

for (size_t i = 0; i < a.size(); ++i) {

combined += a[i];

if (i != a.size() - 1) {

combined += " ";

return combined;

Bài 5 #include <vector>

#include <algorithm>

string combine(vector<string>& words)

sort(words.begin(), words.end(), greater<string>());

string combined;

for (size_t i = 0; i < words.size(); ++i) {

combined += words[i];

if (i != words.size() - 1) {
combined += " ";

return combined;

}\

Bài 4 #include <iostream>

#include <fstream>

using namespace std;

int main()

string filename;

int count = 1;

cin >> filename;

ifstream file(filename);

if (!file.is_open())

cout << "Mission failed";

else

while (getline(file, filename))

count++;

cout << count;

file.close();

return 0;

Bài 3 #include <iostream>

#include <fstream>
using namespace std;

int main()

string filename;

cin >> filename;

ifstream file(filename);

if (!file.is_open())

cout <<"Mission failed!";

int min_val;

int max_val;

int number;

int count = 0;

int S = 0;

if (file >> number)

min_val = max_val = number ;

S+=number;

count++;

while (file >> number)

max_val = max(max_val, number);

min_val = min(min_val, number);

S += number;

count++;

cout << count << " " << min_val << " " << max_val << " " << S;

return 0;

Bài 2
#include <iostream>

#include <fstream>

using namespace std;

int main()

string filename;

cin >> filename;

ifstream file(filename);

if (!file.is_open())

cout << "Mission failed";

int max_val;

int min_val;

if (file >> max_val)

min_val = max_val;

int number;

while (file >> number)

max_val = max(number, max_val);

min_val = min(number, min_val);

cout << max_val << " " << min_val;

file.close();

Bài 1 #include <iostream>

#include <fstream>

using namespace std;

int main()

{
string filename;

cin >> filename;

ifstream file(filename);

if (!file.is_open())

cout << "NO";

else cout << "YES";

return 0;

Node

void print(Node* head)

// Your code here

while (head != NULL) {

cout << head->value << " ";

head = head->next;

Node* insertHead(Node* head, int value)

// Your code here

Node* new_node = new Node();

new_node->value = value;

new_node->next = head;

return new_node;

Node* insertTail(Node* head, int value)

// Your code here


Node* new_node = new Node();

new_node->value = value;

if (head == NULL) return new_node;

Node* i = head;

for (; i->next != NULL; i=i->next);

i->next = new_node;

return head;

Node* deleteNode(Node* head, int pos)

// Your code here

if (pos == 0) {

Node* next = head->next;

delete head;

return next;

Node* i = head;

while (--pos) i = i->next;

Node* deleted = i->next;

i->next = deleted->next;

delete deleted;

return head;

int getValue(Node* head, int pos)

// Your code here

while (pos--) head = head->next;

return head->value;

Bài 2 .bool compareLists(Node* headA, Node* headB) {


// Your code here

while (headA != nullptr && headB != nullptr)

if (headA -> value != headB -> value)

return false;

headA = headA -> next;

headB = headB -> next;

if (headA != nullptr || headB != nullptr)

return false;

return true; // Change this line

Bài 3 vector<int> linkedListToVector(Node* head)

// Your code here

vector <int> mang;

while (head != nullptr)

mang.push_back(head -> value);

head = head -> next;

return mang; // Change this line

Node* vectorToLinkedList(vector<int> values)

// Your code here

Node* head = nullptr;


Node* tail = nullptr;

for (int value: values)

Node* newNode = new Node{value, nullptr};

if (head == nullptr)

head = newNode;

tail = newNode;

else {

tail -> next = newNode;

tail = newNode;

return head; // Change this line

vector<int> linkedListToVector(Node* head)

// Your code here

vector<int> ret;

while (head) {

ret.push_back(head->value);

head = head->next;

return ret;

Node* vectorToLinkedList(const vector<int> &values, int i) {

if (i >= int(values.size())) return NULL;

Node* new_node = new Node();

new_node->value = values[i];
new_node->next = vectorToLinkedList(values, i+1);

return new_node;

Node* vectorToLinkedList(vector<int> values)

return vectorToLinkedList(values, 0);

Bài 4 Node* extractNodes(Node* head, int threshold)

// Your code here

Node* newhead = nullptr;

Node* newtail = nullptr;

while (head != nullptr)

if (head -> value < threshold)

Node* newNode = new Node{head -> value, nullptr};

if (newhead == nullptr)

newhead = newNode;

newtail = newNode;

else{

newtail -> next = newNode;

newtail = newNode;

head = head -> next;

return newhead; // Change this line

}
Bài 5 Node* concat(vector<Node*> heads)

Node* result = nullptr;

Node* tail = nullptr;

for (size_t i = 0; i < heads.size(); i++) {

Node* temp = heads[i];

while (temp != nullptr) {

Node* newNode = new Node();

newNode->value = temp->value;

newNode->next = nullptr;

if (result == nullptr) {

result = newNode;

tail = newNode;

} else {

tail->next = newNode;

tail = newNode;

temp = temp->next;

return result;

Bài 7 Node* deleteDuplicates(Node* head)

// Your code here

if (head == NULL) return head;

for (Node* it = head; it != NULL; it=it->next) {

while (it->next && it->next->value == it->value) {

Node* tmp = it->next;

it->next = tmp->next;
delete tmp;

return head;

Node* insert(Node* head, int value)

// Your code here

if (head != NULL && head->value < value) {

head->next = insert(head->next, value);

return head;

Node* new_node = new Node();

new_node->value = value;

if (head == NULL) return new_node;

new_node->next = head;

return new_node;

Bài 8 Node* convert(Node* head)

// Your code here

if (head == NULL) return NULL;

Node* fast = head, *slow = head;

Node* prev_slow = NULL;

while (fast != NULL) {

prev_slow = slow;

fast = fast->next->next;

slow = slow->next;

}
Node* a = head, *b = slow;

prev_slow->next = NULL; // cut the two arrays

while (b != NULL) {

Node* a2 = a->next;

a->next = b;

b = b->next;

a = a->next;

a->next = a2;

a = a->next;

return head;

You might also like