A:
思路:满26进1即可,注意出现字母Z的情况
代码:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <array>
#include <ctime>
#include <cstring>
#define endl "\n"
#define ture true;
#define flase false;
using namespace std;
char str[200];
bool check()
{
int i = 1;
if (str[1] < '0' || str[1] > '9')
return false;
for (; str[i] >= '0' && str[i] <= '9'; i++);
return str[i];
}
int main()
{
int n;
cin >> n;
while (n--)
{
cin >> str;
if (check())
{
int a = 0, b = 0;
int i = 1;
for (; str[i] && str[i]>= '0' && str[i]<= '9'; i++)
{
a *= 10;
a += str[i] - '0';
}
i++;
for (; str[i] && str[i] >= '0' && str[i] <= '9'; i++)
{
b *= 10;
b += str[i] - '0';
}
stack<char> sc;
while (b)
{
char ch = 'A' + (((b % 26) + 25) % 26);
sc.push(ch);
b /= 26;
if (ch == 'Z')
b--;
}
while (!sc.empty())
{
cout << sc.top();
sc.pop();
}
cout << a << endl;
}
else
{
int idx = 0, rst = 0;
while (str[idx] < '0' || str[idx] > '9')
{
rst *= 26;
rst += str[idx] - 'A' + 1;
idx++;
}
cout << "R" << str + idx << "C" << rst << endl;
}
}
return 0;
}
B:
BFS找最短路径即可
代码:
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
int n;
int sx, sy, ex, ey;
char g[N][N];//地图
int d[N][N];//每一个点的距离
PII q[N * N];//手写队列
int bfs()
{
int hh = 0, tt = 0;
q[0] = { sx - 1, sy - 1 };
memset(d, -1, sizeof d);
d[sx - 1][sy - 1] = 0;
int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0 ,1, 0, -1 };
while (hh <= tt)
{
auto t = q[hh++];
for (int i = 0; i < 4; i++)
{
int x = t.first + dx[i], y = t.second + dy[i];
if (x >= 0 && x < n && y >= 0 && y < n && g[x][y] == '0' && d[x][y] == -1)
{
d[x][y] = d[t.first][t.second] + 1;
q[++tt] = { x, y };
}
}
}
return d[ex - 1][ey - 1];
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> g[i];
cin >> sx >> sy >> ex >> ey;
cout << bfs() << endl;
return 0;
}
C:
由于数据过大,不能用int将数据转换为二进制再运算,因此可以用字符串来进行存储
加法:for (int k = n; s[–k] != ‘0’ && (s[k] = ‘0’) || !(s[k] = ‘1’); );
减法:for (int k = n; s[–k] != ‘1’ && (s[k] = ‘1’) || !(s[k] = ‘0’); );
乘法:s[n++] = ‘0’
除法:s[–n] = ‘\0’
代码:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <array>
#include <ctime>
#include <cstring>
#define endl "\n"
#define ture true;
#define flase false;
using namespace std;
char s[100000000] = { 0 }, oper[6000000] = { 0 };
int main()
{
int n, m;
scanf("%d%d%s%s", &n, &m, s, oper);
for (int i = 0; i < m; i++)
switch (oper[i])
{
case '*': s[n++] = '0'; break;
case '/': s[--n] = '\0'; break;
case '+': for (int k = n; s[--k] != '0' && (s[k] = '0') || !(s[k] = '1'); ); break;
case '-': for (int k = n; s[--k] != '1' && (s[k] = '1') || !(s[k] = '0'); ); break;
}
puts(s);
}
D:
二分,记忆力的值
check函数按照题目要求即可
代码:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <array>
#include <ctime>
#include <cstring>
#define endl "\n"
#define ture true;
#define flase false;
using namespace std;
long long n, k, a[100005];
bool check(long long x) {
long long tot = 1, sum = 0;
for (long long i = 1; i <= n; i++) {
if (a[i] > x) return 0;
if (sum + a[i] > x) {
sum = 0;
tot++;
}
sum += a[i];
}
return tot <= k;
}
signed main() {
scanf("%lld%lld", &n, &k);
for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]), a[i] *= a[i];
long long l = 1, r = 1e18;
while (l <= r) {
long long mid = (l + r) >> 1;
if (check(mid)) r = mid - 1;
else l = mid + 1;
}
printf("%lld\n", l);
return 0;
}
E:
使用dfs深搜即可,注意最后输出的格式
代码:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <array>
#include <ctime>
#include <cstring>
#define endl "\n"
#define ture true;
#define flase false;
using namespace std;
int a[5], ans[4][4];
bool ok;
void print() //输出
{
for (int i = 1; i <= 3; i++)
{
if (ans[i][1] < ans[i][3])
swap(ans[i][1], ans[i][3]); //注意题目中说两个数必须先输出大的再输出小的
printf("%d", ans[i][1]); //输出第一个数字
if (ans[i][2] == 1) printf("+");
if (ans[i][2] == 2) printf("-");
if (ans[i][2] == 3) printf("*");
if (ans[i][2] == 4) printf("/"); //输出运算符号
printf("%d", ans[i][3]);
if (ans[i][2] == 1) printf("=%d\n", ans[i][1] + ans[i][3]);
if (ans[i][2] == 2) printf("=%d\n", ans[i][1] - ans[i][3]);
if (ans[i][2] == 3) printf("=%d\n", ans[i][1] * ans[i][3]);
if (ans[i][2] == 4) printf("=%d\n", ans[i][1] / ans[i][3]); //输出结果
}
}
bool check() //判断是否组成了24
{
for (int i = 1; i <= 4; i++)
if (a[i] == 24) return 1;
return 0;
}
void dfs(int k)
{
if (k == 4) //搜索完毕
{
if (check())
{
print();
ok = 1;
}
return;
}
int x, y;
for (int i = 1; i <= 4; i++)
for (int j = 1; j <= 4; j++) //枚举两个数字
if (i != j && a[i] > 0 && a[j] > 0)
{
ans[k][1] = a[i]; //加
ans[k][2] = 1;
ans[k][3] = a[j];
x = a[i], y = a[j];
a[i] = x + y;
a[j] = -1;
dfs(k + 1);
a[i] = x;
a[j] = y;
if (ok) return;
ans[k][1] = a[i]; //减
ans[k][2] = 2;
ans[k][3] = a[j];
x = a[i], y = a[j];
a[i] = x - y;
a[j] = -1;
dfs(k + 1);
a[i] = x;
a[j] = y;
if (ok) return;
ans[k][1] = a[i]; //乘
ans[k][2] = 3;
ans[k][3] = a[j];
x = a[i], y = a[j];
a[i] = x * y;
a[j] = -1;
dfs(k + 1);
a[i] = x;
a[j] = y;
if (ok) return;
if (a[j] > 0 && a[i] % a[j] == 0) //除,注意要判断能否整除
{
ans[k][1] = a[i];
ans[k][2] = 4;
ans[k][3] = a[j];
x = a[i], y = a[j];
a[i] = x / y;
a[j] = -1;
dfs(k + 1);
a[i] = x;
a[j] = y;
if (ok) return;
}
}
}
int main()
{
scanf("%d%d%d%d", &a[1], &a[2], &a[3], &a[4]);
dfs(1);
if (!ok) cout << "No answer!";
return 0;
}