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

C++ he thong

Uploaded by

Toàn Vũ Thế
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)
6 views

C++ he thong

Uploaded by

Toàn Vũ Thế
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/ 9

Contents

Bài 1:.................................................................................1
Bài 2:.................................................................................3
Bài 4:.................................................................................7

Bài 1:
#include <bits/stdc++.h>

using namespace std;

long long F[101];

void Fibo() {
F[0] = 0;
F[1] = 1;
for (int i = 2; i <= 100; i++) {
F[i] = F[i - 2] + F[i - 1];
}
}
int bn(int l, int r, int x) {
while (l <= r) {
int m = (l + r) / 2;
if (F[m] == x) {
return 1;
} else if (F[m] > x) {
r = m - 1;
} else {
l = m + 1;
}
}
return -1;
}

int solve(int& n) {
n++;
while (1) {
if (bn(0, 100, n) == -1) {
return n;
} else {
n++;
}
}
}

int main() {
Fibo();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
cout << solve(n) << endl;
}
return 0;
}

Bài 2:
#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;

int a, b;
int res = -1;

for (int i = 0; i < s.size(); i++) {


for (int j = i; j < s.size(); j++) {
a = 0;
b = 0;

for (int k = i; k <= j; k++) {


if (s[k] == '1') {
b++;
}
if (s[k] == '0') {
a++;
}
}

if (a - b > res) {
res = a - b;
}
}
}

cout << res << endl;


}
return 0;
}

Bài 3:
#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;

int F1 = 1, F2 = 2, F3 = 3;

while (n > 0) {
F1 = F2;
F2 = F3;
F3 = F1 + F2;

n = n - (F3 - F2 - 1);
}

n = n + (F3 - F2 - 1);
cout << F2 + n << endl;
}
return 0;
}

Bài 4:
#include <bits/stdc++.h>

using namespace std;

const int MAX_SIZE = 55;


long long F[MAX_SIZE];
vector<long long> a;

void Fibo() {
F[0] = 0;
F[1] = 1;

for (int i = 2; i <= 50; i++) {


F[i] = F[i - 1] + F[i - 2];
if (F[i] % 2 == 0) {
a.push_back(F[i]);
}
}
}

int main() {
Fibo();

int t;
cin >> t;

while (t--) {
long long n;
cin >> n;

long long rs = 0;

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


if (a[i] <= n) {
rs += a[i];
}
}

cout << rs << endl;


}

return 0;
}

You might also like