DAP AN DE SO 6
CÂU 1. Phô tô
include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL); cout.tie(NULL);
if (fopen("BAI1.INP","r")){
freopen ("BAI1.INP", "r", stdin);
freopen ("BAI1.OUT", "w", stdout);
}
long long n,res = 0;
cin>>n;
if ((n/2)<=100) res+=300(n/2);
else res+=280(n/2);
if ((n%2)!=0) res+=250;
cout<<res;
return 0;
}
CÂU 2. Hamming
include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL); cout.tie(NULL);
if (fopen("BAI2.INP","r")){
freopen ("BAI2.INP", "r", stdin);
freopen ("BAI2.OUT", "w", stdout);
}
string x,y;
int res = 0;
cin>>x>>y;
while (x.size()<y.size()) x = '0'+x;
while (x.size()>y.size()) y = '0'+y;
for (int i=0;i<x.size();i++)
if (x[i]!=y[i]) res++;
cout<<res;
return 0;
}
CÂU 3. Tích nguyên tố
include <bits/stdc++.h>
using namespace std;
define Amax 1000000
int p[Amax+1],n;
void era(){
p[0]=p[1]=1;
for (long long i = 2; ii<=Amax; i++)
if (p[i]==0)
for (long long j = ii; j<=Amax; j+=i)
p[j] = 1;
}
void sub1(){
int a[101],cnt=0;
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++){
int x = a[i]*a[j];
bool nt = x>1;
for (int u = 2; u<=sqrt(x);u++)
if (x%u==0) nt = false;
if (nt) cnt++;
}
cout<<cnt;
}
int main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL); cout.tie(NULL);
if (fopen("BAI3.INP","r")){
freopen ("BAI3.INP", "r", stdin);
freopen ("BAI3.OUT", "w", stdout);
}
int a;
cin>>n;
era();
long long n1=0,nt=0,res=0;
for (int i = 1; i<=n; i++){
cin>>a;
if (a==1){
res += nt;
n1++;
}
if (p[a]==0){
res += n1;
nt++;
}
}
cout<<res;
return 0;
}
CÂU 4. Dãy ngoặc đúng
include <bits/stdc++.h>
using namespace std;
string s;
void sub1(){
bool ok;
int cnt=0,d;
for (int i=0;i<s.size()-1;i++)
for (int j=i+1;j<s.size();j++){
d=0;ok=true;
for (int k=i;k<=j;k++){
if (s[k]=='(') d++;
else d--;
if (d<0) {
ok = false; break;
}
}
if (ok && d==0) cnt++;
}
cout<<cnt;
}
void sub2(){
bool ok;
int cnt=0,d=0;
for (int i=0;i<s.size()-1;i++){
if (s[i]=='(') d=1;
else continue;
for (int j=i+1;j<s.size();j++){
if (s[j]=='(') d++;
else d--;
if (d==0) cnt++;
if (d<0) break;
}
}
cout<<cnt;
}
int main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL); cout.tie(NULL);
if (fopen("BAI4.INP","r")){
freopen ("BAI4.INP", "r", stdin);
freopen ("BAI4.OUT", "w", stdout);
}
cin>>s;
sub2();
return 0;
}
------------HẾT------------
Bình luận