A - Sequence of Strings
Problem Statement
You are given N strings S1,S2,…,SN in this order.
Print SN,SN−1,…,S1 in this order.
Constraints
- 1≤N≤10
- N is an integer.
- Si is a string of length between 1 and 10, inclusive, consisting of lowercase English letters, uppercase English letters, and digits.
Input
The input is given from Standard Input in the following format:
N S1 S2 ...... SN
Output
Print N lines. The i-th (1≤i≤N) line should contain SN+1−i.
Sample Input 1
3
Takahashi
Aoki
Snuke
Sample Output 1
Snuke
Aoki
Takahashi
We have N=3 .S1= Takahashi
,S2= Aoki
, and S3= Snuke
.
Thus, you should print Snuke
, Aoki
, and Takahashi
in this order.
Sample Input 2
4
2023
Year
New
Happy
Sample Output 2
Happy
New
Year
2023
The given strings may contain digits.
AC Code:
逆序输出即可!
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
stack<string> sta;
string s;
int n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cin >> n;
while (n--)
{
cin >> s;
sta.push(s);
}
while (!sta.empty())
{
cout << sta.top() << '\n';
sta.pop();
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector <string> s(n);
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = n - 1; i >= 0; i--) cout << s[i] << '\n';
return 0;
}
B - Multi Test Cases
Problem Statement
In this problem, an input file contains multiple test cases.
You are first given an integer T. Solve the following problem for TT test cases.
- We have N positive integers A1,A2,...,AN. How many of them are odd?
Constraints
- 1≤T≤100
- 1≤N≤100
- 1≤Ai≤10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format, where test-i represents the i-th test case:
T test1 test2 ...... testT
Each test case is in the following format:
N A1......AN
Output
Print T lines. The i-th line should contain the answer for the i-th test case.
Sample Input 1
4
3
1 2 3
2
20 23
10
6 10 4 1 5 9 8 6 5 1
1
1000000000
Sample Output 1
2
1
5
0
AC Code:
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
const int N = 1e2 + 5;
int a[N], sum;
int t, n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cin >> t;
while (t--)
{
sum = 0;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
if (a[i] & 1)
sum++;
cout << sum << '\n';
}
return 0;
}
C - Count Connected Components
Problem Statement
You are given a simple undirected graph with N vertices numbered 1 to N and M edges numbered 1 to M. Edge i connects vertex ui and vertex vi.
Find the number of connected components in this graph.
Notes
A simple undirected graph is a graph that is simple and has undirected edges.
A graph is simple if and only if it has no self-loop or multi-edge.
A subgraph of a graph is a graph formed from some of the vertices and edges of that graph.
A graph is connected if and only if one can travel between every pair of vertices via edges.
A connected component is a connected subgraph that is not part of any larger connected subgraph.
Constraints
- 1≤N≤100
- The given graph is simple.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M
u1 v1
u2 v2
......
uM vM
Output
Print the answer.
Sample Input 1
5 3
1 2
1 3
4 5
Sample Output 1
2
The given graph contains the following two connected components:
- a subgraph formed from vertices 1, 2, 3, and edges 1, 2;
- a subgraph formed from vertices 4, 5, and edge 3.
Sample Input 2
5 0
Sample Output 2
5
Sample Input 3
4 6
1 2
1 3
1 4
2 3
2 4
3 4
Sample Output 3
1
连通块的数量
AC Code:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 10;
int n, m, p[N];
int find(int x)
{
if (p[x] != x)
p[x] = find(p[x]);
return p[x];
}
signed main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
p[i] = i;
while (m--)
{
int u, v;
cin >> u >> v;
u = find(u), v = find(v);
if (u != v)
p[u] = v;
}
int ans = 0;
for (int i = 1; i <= n; i++)
ans += find(i) == i;
cout << ans << '\n';
}
D - Happy New Year 2023
Problem Statement
There is an integer N. It is known that N can be represented as N=p^2*q using two different prime numbers p and q. Find p and q.You have T test cases to solve.
Input
The input is given from Standard Input in the following format, where test-i represents the i-th test case:
T
test1
test2
test3
......
testT
1<=T<=10.
1<=N<=9*10^18.
Each test case is in the following format:
N
Output
Print T lines.
Sample Input 1
3
2023
63
1059872604593911
Sample Output 1
17 7
3 7
104149 97711
AC Code:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e6 + 10;
int n, cnt, primes[N];
bool st[N];
void get_primes()
{
for (int i = 2; i < 3e6; i++)
{
if (!st[i])
primes[cnt++] = i;
for (int j = 0; primes[j] * i < 3e6; j++)
{
st[primes[j] * i] = true;
if (i % primes[j] == 0)
break;
}
}
}
void solve()
{
cin >> n;
for (int i = 0; i < cnt; i++)
{
if (n % (primes[i] * primes[i]) == 0)
{
cout << primes[i] << " " << n / (primes[i] * primes[i]) << '\n';
return;
}
if (n % primes[i] == 0)
{
int num = n / primes[i];
int x = (int)sqrt(num);
cout << x << " " << primes[i] << '\n';
return;
}
}
}
signed main()
{
get_primes();
st[1] = 1;
int T;
cin >> T;
while (T--)
solve();
}