0% found this document useful (0 votes)
16 views9 pages

Bai Tap C++

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)
16 views9 pages

Bai Tap C++

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

//Doi sang he La Ma

#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string s;
string cs[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX",
"V", "IV", "I"};
int gt[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
while (cin >> n) {
s = "";
int i = 0;
while (n > 0) {
while (n >= gt[i]) {
s += cs[i];
n -= gt[i];
}
i++;
}
cout << s << endl;}
return 0;
}
//Dong dieu vs day mau
#include <bits/stdc++.h>
using namespace std;
bool check(int t, int n, int a[], int b[]) {
int k = b[t] - a[0];
for (int i = 1; i < n; i++) {
if (b[t + i] != a[i] + k) return false;
}
return true;
}
int main() {
int n;
cin >> n;
int dem = 0, a[100005], b[100005];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int i = 0;
while (cin >> b[i]) {
i++;
}
int m = i;
i = 0;
while (i <= m - n) {
if (check(i, n, a, b)) {
dem++;
i += n;
} else {
i += 1;
}
}
cout << dem << endl;
return 0;
}
//Cum Bieu thuc
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
getline(cin, s);
int n = s.size();
vector<string> l;
for (int i = 0; i < n; i++) {
if (s[i] == ')') {
int sd = 1, sum = 0;
for (int j = i - 1; j >= 0; j--) {
if (s[j] == ')') sd++;
if (s[j] == '(') {
sum++;
if (sum == sd) {
l.push_back(s.substr(j, i - j + 1));
break;
}
}
}
}
}
int m = l.size();
cout << m << endl;
for (int i = 0; i < m; i++) {
cout << l[i] << endl;
}
return 0;
}
//So nguyen to tron
#include <bits/stdc++.h>
using namespace std;
bool snt(long long n) {
if(n<2) return false;
for (long long i=2;i<=sqrt(n);i++) {
if(n%i==0) return false;
}
return true;
}
string daoxau(string s) {
if (s.empty()) return s;
return s.substr(1) + s[0];
}
bool doi(int n) {
string s = to_string(n);
int dd = s.length();
for (long long i = 0; i < dd; i++) {
if (!snt(stoi(s))) {
return false;
}
s = daoxau(s);
}
return true;
}
int main() {
long long N;
cin >> N;
if (N < 2) {
cout << 0;
return 0;
}
long long dem = 0;
for (int i = 2; i < N; i++) {
if (doi(i)) {
dem++;
}
}
cout << dem;
return 0;
}
// Bài toán khuyến mãi: Mua a quyển sách thì đc tặng b quyển. Mỗi quyển sách
giá c đồng. Tính số tiền ít nhất để mua n quyển sách
Giải:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,a,b,c;
cin>>a>>b>>c>>n;
int k=n/(a+b);//so lan mua
int t=n%(a+b);//so quyen sach
if(t>a) t=a;
long long s=k*a*c+t*c;
cout<<s<<endl;
return 0;
}
// Cho day so gom n so nguyen duong lan luot la a1,…,an. Moi phan tu ai dc
phep bien doi bang ccah tang hoac iam xuong 1 don vi. Xac dinh so lg phan tu
bi bien doi it nhat sao cho tong hai phan tu bat ki trong day luon la so chan.
#include<bits/stdc++.h>
using namespace std;
int a[100005];
int main(){
int n,chan=0,le=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]%2==0) chan++;
else le++;}
cout<<min(chan,le);
return 0;
}
//Trong tiethoc toan co giao day An ve cac phep toan +,-,*. Vi An con nho nen
chi thuc hien cac phep toan tren voi 1 chu so. Tinh gia tri bieu thuc
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
if(s[0]=='-'|s[0]=='+'){
s="0"+s;
}
int n=s.size(),p=0,t=s[0]-48;
for(int i=1;i<n;i+=2){
if(s[i]=='*') t*=s[i+1]-48;
else {
p+=t;
t=s[i+1]-48;
if(s[i]=='-') t-=t;
}
}
cout<<p+t;
return 0;
}
//
#include<bits/stdc++.h>
using namespace std;
int a[105];
int main(){
int n,k,dem=0;
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<n;i++)
for(int j=i+1;j<n;j++)
if(abs(a[i]+a[j]==k)) dem++;
cout<<dem;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100;
int m, n, q;
int a[MAXN][MAXN];
int sum[MAXN][MAXN];
int main() {
cin >> m >> n >> q;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
sum[i][j] = sum[i][j - 1] + a[i][j];
}
}
for (int j = 1; j <= n; j++) {
for (int i = 1; i <= m; i++) {
sum[i][j] += sum[i - 1][j];
}
}
for (int k = 1; k <= q; k++) {
int x, y, u, v;
cin >> x >> y >> u >> v;
int total = sum[u][v] - sum[u][y - 1] - sum[x - 1][v] + sum[x - 1][y - 1];
cout << total << endl;
}
return 0;
}https://fr.slideshare.net/slideshow/bi-tp-cu-trc-d-liu-2/238778107

You might also like