some example for algorithms, Exercises of Algorithms and Programming

some example for algorithms

Typology: Exercises

2020/2021

Uploaded on 08/04/2021

duc-tran-2
duc-tran-2 🇻🇳

1 document

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C1:
#include <bits/stdc++.h>
using namespace std;
int n = 0, a[100010];
void input(string s) {
for (int i = 0; i < s.size(); i++) {\
int x = 0;
while (s[i] != ',' && i < s.size()) {
x = x*10 + s[i]-'0';
i++;
}
//cout <<"s[i] " << s[i] << endl;
i++;
n++;
a[n] = x;
}
}
int main()
{
string s;
getline(cin, s);
input(s);
int Maxn = INT_MIN, Minn = INT_MAX, tong = 0, soluong = 0;
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download some example for algorithms and more Exercises Algorithms and Programming in PDF only on Docsity!

C1:

#include <bits/stdc++.h> using namespace std; int n = 0, a[100010]; void input(string s) { for (int i = 0; i < s.size(); i++) {
int x = 0; while (s[i] != ',' && i < s.size()) { x = x*10 + s[i]-'0'; i++; } //cout <<"s[i] " << s[i] << endl; i++; n++; a[n] = x; } } int main() { string s; getline(cin, s); input(s); int Maxn = INT_MIN, Minn = INT_MAX, tong = 0, soluong = 0;

for (int i = 1; i <= n; i++) { Maxn = max(Maxn, a[i]); Minn = min(Minn, a[i]); if (a[i] % 2 == 0) tong += a[i]; else soluong++; } cout << Minn << "\n" << Maxn << "\n" << tong << "\n" << soluong; }

C2:

#include <bits/stdc++.h> using namespace std; int n = 0, a[100010]; int main() { int test; cin >> test; while (test--) {

void input(string s) { for (int i = 0; i < s.size(); i++) {
int x = 0; while (s[i] != ',' && i < s.size()) { x = x10 + s[i]-'0'; i++; } //cout <<"s[i] " << s[i] << endl; i++; n++; a[n] = x; } } int main() { string s; getline(cin, s); input(s); double trungbinh = 0; for (int i = 1; i <= n; i++) trungbinh += a[i]; trungbinh=trungbinh/(double)n; double phuongsai = 0; for (int i = 1; i <= n; i++) phuongsai += ((double)a[i] - trungbinh)((double)a[i] - trungbinh); phuongsai = phuongsai/(double)n; cout << trungbinh << "\n" << phuongsai; }

C4:

#include <bits/stdc++.h> using namespace std; int n = 0, a[100010], dem[1000], maxdem = 0; // dem[i] la so luong phan tu co gia tri la i o trong day int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; dem[a[i]]++; maxdem = max(maxdem, dem[a[i]]); } cout << n - maxdem; }

cout << "yes"; return 0; } } } cout << "no"; }

C6:

#include <bits/stdc++.h> using namespace std; int n, m, max_a = INT_MIN, min_b = INT_MAX, res = 0;

int a[500010], b[500010]; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i];A max_a = max(max_a, a[i]); } for (int i = 1; i <= m; i++) { cin >> b[i]; min_b = min(min_b, b[i]); } for (int x = max_a; x <= min_b; x++) { bool ok = 1; for (int i = 1; i <= n; i++) { if (x % a[i] != 0) { ok = 0; break; } } if (ok == 0) continue; ok = 1; for (int i = 1; i <= m; i++) { if (b[i] % x != 0) { ok = 0; break; } } if (ok == 0)

cout << "Yes"; }

C8:

#include <bits/stdc++.h> using namespace std; int n, m; string s; double a[500010], b[500010]; int main() { cin >> n >> m; cin >> s; s = ' ' + s; for (int truyvan = 1; truyvan <= m; truyvan++) { int i, j; cin >> i >> j; string str = ""; int dodai = j-i+1, res = 1;

for (int x = i; x <= j; x++) str += s[x]; for (int x = j+1; x < s.size(); x++) { string temp = ""; int t = 1; while(t <= dodai && x < s.size()) { t++; temp += s[x]; x++; } if (x >= s.size()) break; x--; if (temp == str) res++; else break; } cout << res << '\n'; } }

C10:

#include <bits/stdc++.h> using namespace std; char c[100]; int main() { string s; cin >> s; for (int i = 0; i < s.size(); i++) c[i+1] = s[i]; for (int i = 1; i <= s.size(); i++) { string temp1 = ""; temp1 += c[i]; for (int j = 1; j <= s.size(); j++) { string temp2 = temp1 + c[j]; cout << temp2 << " ";

for (int x = 1; x <= s.size(); x++) { string temp3 = temp2 + c[x]; cout << temp3 << " "; } } } }

C11:

#include <bits/stdc++.h> using namespace std; char c[100]; int main() { int n; cin >> n; for (int i = 1;i <= n; i++) { for (int j = i; j <= n; j++) cout << j <<" "; for (int j = 1; j <= i-1; j++) cout << j <<" "; cout << "\n";

cin >> n >> q; for (int i = 0; i < n; i++) { cin >> s[i]; for (int j = 0; j < s[i]; j++) { int x; cin >> x; a[i].push_back(x); } } for (int i = 1; i <= q; i++) { int x, y; cin >> x >> y; cout << a[x][y] << '\n'; } }

C13:

#include <bits/stdc++.h>

using namespace std; int main() { map <string, int> dd; // dd[s] = true thi tuc la xau s co xuat hien trong day, neu = false thi la ko xuat hien int n; string s[110]; cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; dd[s[i]]++; } for (int i = 1; i <= n; i++) { if (s[i].size()%2 == 0) continue; string t = ""; // t la xau dao nguoc cua s[i] for (int j = s[i].size()-1; j >= 0; j--) t += s[i][j]; if (t == s[i]){ if (dd[s[i]] >= 2) { cout << s[i].size() << " " << s[i][(s[i].size()/2)]; return 0; } continue; } if (dd[t] == true) { cout << s[i].size() << " " << s[i][(s[i].size()/2)]; return 0; } } }