算法入门-构造算法

BGN72 茉茉的密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//茉茉的密码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
#define sz size()
int main () {
ios_base::sync_with_stdio (false);
cin.tie (0);
map<char,ll> m;

int n;
cin >> n;
vector<string> v(n);
for (int i=0;i<n;i++) cin >> v[i];
for (int i=0;i<n;i++) {
map<char,ll> mo;
for (int j=0;j<v[i].sz;j++) {
if(!mo[v[i][j]]){
mo[v[i][j]]++;
m[v[i][j]]++;
}
}
}
for (const auto s: m) {
if (s.second ==n) {
cout << s.first;
return 0;
}
}
}

BGN73

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main() {
int x;
cin>>x;
if(x==1){
cout<<-1;
return 0;
}
cout<<x<<' '<<x;
}

BGN74

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz size()
#define vll vector<long long>
using ll=long long;
using ull = unsigned long long;
const int MOD1=1e9+7;
const int MOD2=998244353;
void solve() {
ll n,k;
cin>>n>>k;
vector<vector<int>> v(n,vector<int>(n,0));
for (int i=0;i<n;i++) {
v[i][i]=k;
}
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
cout<<v[i][j]<<" ";
}
cout<<'\n';
}
}

int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
int t=1;
//cin>>t;
while (t--) {
solve();
}
}

BGN76

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// Created by XIAOBAI on 2026/6/2.
//
#include <bits/stdc++.h>
using namespace std;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
using ll = long long;
using ull = unsigned long long;
bool cmp(int a,int b) {
return a>b;
}

ll get(ll x) {
if (x==0) return 0;
if (x==1) return 1;
return get(x/2)+get(x%2);
}
void solve() {
ll n;
cin>>n;
cout<<get(n);

}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T=1;
cin>>T;
while(T--) {
solve();
}

}