A.我难道是签到题?{:target=”_blank”}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#define LL long long
using namespace std;
const int N = 15;
double n, p; // 实数!!!
int main()
{
while (~scanf("%lf%lf", &n, &p))
{
printf("%.0lf\n", pow(p, 1 / n));
}
return 0;
}
B.我是迷宫吗?{:target=”_blank”}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#define LL long long
using namespace std;
const int N = 10;
int g[N][N];
int p, q;
int step(int p, int q)
{
int res = 0;
res += abs(p - 3);
res += abs(q - 3);
return res;
}
int main()
{
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++)
{
scanf("%d", &g[i][j]);
if (g[i][j])
p = i, q = j;
}
printf("%d\n", step(p, q));
return 0;
}
bfs写法
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#define LL long long
using namespace std;
const int N = 10;
struct node
{
int x, y;
int step;
};
int g[N][N];
int p, q;
bool vis[N][N];
queue<node> heap;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int bfs(int x, int y)
{
node first;
first.x = x, first.y = y;
first.step = 0;
heap.push(first);
vis[x][y] = true;
while (!heap.empty())
{
node t = heap.front();
heap.pop();
if (t.x == t.y && t.x == 3)
return t.step;
for (int i = 0; i < 4; i++)
{
node next;
next.x = t.x + dx[i], next.y = t.y + dy[i];
next.step = t.step + 1;
if (!vis[next.x][next.y] && next.x >= 1 && next.x <= 5 && next.y >= 1 && next.y <= 5)
{
vis[next.x][next.y] = true;
heap.push(next);
}
}
}
return 0;
}
int main()
{
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++)
{
scanf("%d", &g[i][j]);
if (g[i][j])
p = i, q = j;
}
printf("%d\n", bfs(p, q));
return 0;
}
C.史学长的神奇翻转{:target=”_blank”}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#define N 510
using namespace std;
int n;
int arr[N];
void change(int l, int r)
{
int t = r - l + 1;
if (t % 2)
{
int p = l + r >> 1;
for (int i = l; i <= p; i++)
swap(arr[i], arr[2 * p - i]);
}
else
{
int p = l + r >> 1;
for (int i = l; i <= p; i++)
swap(arr[i], arr[l + r - i]);
}
}
int main()
{
int T;
cin >> T;
while (T--)
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> arr[i];
int l = 0;
for (int i = 1; i <= n; i++)
if (i != arr[i])
{
l = i;
break;
}
int r;
for (int i = l; i <= n; i++)
if (arr[i] == l)
{
r = i;
break;
}
if (l)
change(l, r);
for (int i = 1; i <= n; i++)
if (i == n)
printf("%d\n", arr[n]);
else
printf("%d ", arr[i]);
}
return 0;
}
D.齐学姐的魔镜{:target=”_blank”}
#include <iostream>
#include <algorithm>
#include <cstring>
#define N 100010
using namespace std;
int n;
char str[N];
bool check(int n) {
for (int i = 0; i <= n / 2; i ++ )
if (str[i] != str[n - 1 - i])
return false;
return true;
}
int main() {
scanf("%s", str);
n = strlen(str);
while (n % 2 == 0) {
if (check(n))
n /= 2;
else
break;
}
printf("%d\n", n);
return 0;
}
E.妈妈的妇女节{:target=”_blank”}
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 10;
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
int n, m;
int g[N][N];
bool st[N][N];
PII load[N][N];
void bfs(int a, int b) {
queue<PII> q;
PII One, next;
One.first = a, One.second = b;
q.push(One);
st[a][b] = true;
while (q.size()) {
PII t = q.front();
q.pop();
for (int i = 0; i < 4; i ++ ) {
next.first = t.first + dx[i], next.second = t.second + dy[i];
if (next.first >= 0 && next.first <= 4 && next.second >= 0 && next.second <= 4 && !st[next.first][next.second]
&& !g[next.first][next.second]) {
st[next.first][next.second] = true;
q.push(next);
load[next.first][next.second] = t;
}
}
}
}
void output(PII s) {
if (s.first == s.second && s.first == 0) {
printf("(0, 0)\n");
return;
}
output(load[s.first][s.second]);
printf("(%d, %d)\n", s.first, s.second);
}
int main() {
for (int i = 0; i < 5; i ++ )
for (int j = 0; j < 5; j ++ )
cin >> g[i][j];
bfs(0, 0);
PII ans;
ans.first = ans.second = 4;
output(ans);
return 0;
}
F.逗乐郭学长{:target=”_blank”}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#define LL long long
using namespace std;
const int N = 10;
int n, m;
int f(int n, int m){
if (n == 1 || m == 1 || !n || !m)
return 1;
if (n < m)
return f(n, n);
else return f(n, m - 1) + f(n - m, m);
}
int main()
{
int T;
scanf("%d", &T);
while (T -- ){
scanf("%d%d", &n, &m);
printf("%d\n", f(n, m));
}
return 0;
}
G.水一道算一道{:target=”_blank”}
#include <iostream>
#include <cstring>
#include <algorithm>
const int N = 5e4 + 10;
typedef long long LL;
int a[N];
LL s[N];
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i ++ )
scanf("%d", &a[i]);
for (int i = 1; i <= n; i ++ )
s[i] = s[i - 1] + a[i];
int q;
scanf("%d", &q);
while (q -- )
{
int l, r;
scanf("%d%d", &l, &r);
printf("%lld\n", s[l + r - 1] - s[l - 1]);
}
return 0;
}
H. 大一萌新的身高问题{:target=”_blank”}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#define LL long long
using namespace std;
const int N = 10010;
int b[N];
int h, n, m, q;
bool vis[N][N];
int main()
{
scanf("%d%d%d%d", &n, &q, &h, &m);
while (m--)
{
int l, r;
scanf("%d%d", &l, &r);
if (l > r)
swap(l, r);
if (vis[l][r])
continue;
else
vis[l][r] = true;
b[l + 1]--;
b[r]++;
}
for (int i = 1; i <= n; i++)
b[i] += b[i - 1];
for (int i = 1; i <= n; i++)
printf("%d\n", h + b[i]);
return 0;
}
I. 白学长困境脱险{:target=”_blank”}
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <istream>
#include <cstring>
using namespace std;
char g[15][15];
bool st[15][15];
int n, m;
bool flag;
int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};
struct node {
int a, b;
};
bool check(int x, int y) {
if (x >= 1 && x <= n && y >= 1 && y <= m && !st[x][y] && (g[x][y] == '.' || g[x][y] == 'T'))
return true;
return false;
}
void bfs(int x, int y) {
queue<node> q;
node One, next;
One.a = x, One.b = y;
st[x][y] = true;
q.push(One);
while (q.size()) {
node t = q.front();
q.pop();
if (g[t.a][t.b] == 'T') {
flag = true;
return;
}
for (int i = 0; i < 4; i ++ ) {
next.a = t.a + dx[i], next.b = t.b + dy[i];
if (check(next.a, next.b)) {
st[next.a][next.b] = true;
q.push(next);
}
}
}
return;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i ++ )
cin >> g[i];
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
if (g[i][j] == 'S')
bfs(i, j);
if (flag)
puts("yes");
else
puts("no");
return 0;
}
J. 数学题不配放前面吗?{:target=”_blank”}
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define INF 0x3f3f3f3f
const int N = 1e6 + 10;
int primes[N], cnt;
bool st[N];
void get_primes() {
for (int i = 2; i < N; i ++ ) {
if (st[i])
continue;
primes[cnt ++ ] = i;
for (int j = i + i; j < N; j += i)
st[j] = true;
}
}
int main() {
get_primes();
int l, u;
while (cin >> l >> u) {
memset(st, false, sizeof st);
for (int i = 0; i < cnt; i ++ ) {
int a = l / primes[i];
int b = u / primes[i];
for (int j = max(a, 2); j <= b; j ++ )
st[j * primes[i] - l] = true;
}
if (l == 1)
st[0] = true;
int max = -INF, min = INF;
int minl, minr, maxl, maxr;
int mark = -1;
for (int i = 0; i <= u - l; i ++ ) {
if (st[i])
continue;
if (mark == -1) {
mark = i;
continue;
}
if (i - mark > max) {
max = i - mark;
maxl = mark;
maxr = i;
}
if (i - mark < min) {
min = i - mark;
minl = mark;
minr = i;
}
mark = i;
}
if (min == INF || max == -INF)
cout << "There are no adjacent primes." << endl;
else
printf("%d,%d are closest, %d,%d are most distant.\n", minl + l, minr + l, maxl + l, maxr + l);
}
return 0;
}