目录
1.🌟36进制
📕题目描述:
☀️思路:
n进制转换为10进制
从右到左用每个数去乘以n的相应次方,然后将这些数字相加就是了。
例如:
ABC=
✏️代码 :
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<math.h>
using namespace std;
string a;
int n;
int main() {
a = "MANY";
int cnt = 0;
for (int i = a.size() - 1; i >= 0; i--) {
int t = a[i] - 'A' + 10;
n += t * pow(36, cnt);
cnt++;
}
cout << n;
return 0;
}
2.🌟交换瓶子
📕题目描述:
输入
5
3 1 2 5 4
输出
3
☀️思路:
根据题意我们可以看出,目标序列的每个下标与其对应的 瓶子序号相同。
因此我们可以直接对序列进行枚举,当当前瓶子的序号a[i]和其下标i不同时,再从i+1往后进行枚举,找到对应的 a[j] 和的下标 i 相等,然后将两者交换,交换次数 ans+1
✏️代码 :
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e4 + 10 ;
int n;
int a[N];
int main() {
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
if (a[i] != i) {
for (int j = i+1; j <= n; j++) {
if (a[j] == i) {
swap(a[i], a[j]);
ans++;
break;
}
}
}
}
cout << ans;
return 0;
}
3.🌟路径之谜
📕题目描述:
输入:
4
2 4 3 4
4 3 3 3
输出
0 4 5 1 2 3 7 11 10 9 13 14 15
☀️思路:
dfs题,第一次做大致思路和框架写出来了,但是WA了,后来看了题解发现我只在移动每一步时判断了箭靶的箭数是否大于0。 在运行到终点时还要对每个箭靶数量的箭数进行判断,所有箭靶的箭数为0才能输出此路径
✏️代码 :
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<vector>
using namespace std;
const int N = 50 ;
//int mp[N][N];
int n;
bool vis[N][N];//标记是否已走过
int x[N], y[N];//靶子箭数
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int road[N * N]; //储存路径
//(左边)x=下标/4
//(上面) y=下标%4
//每走一步将两靶子箭数量减一
bool check() { //判断到终点时靶子的箭是否用完
for (int i = 0; i < n; i++) {
if (x[i] != 0 || y[i] != 0) return false; //靶子箭数未清零
}
return true;
}
bool flag;
void dfs(int xx, int yy, int step) { //每一次只能走一步,用step来记录步数
road[step] = (xx * n + yy); //加入路径数组
if (x[xx] < 0 || y[yy] < 0) return ;//箭靶数不够(可以不考虑)
if (xx == n - 1 && yy == n - 1) {
flag = 1;
if (!check()) {//到终点后没有用完箭说明此路径非法
flag = 0;
}
if (flag) {
for (auto t : road) {
if (t) cout << t << " ";
}
}
return;
}
for (int i = 0; i < 4; i++) {
int fx = xx + dx[i], fy = yy + dy[i];
if (fx >= 0 && fx < n && fy >= 0 && fy < n && !vis[fx][fy] && x[fx] > 0 && y[fy] > 0) { //在范围内
x[fx]--, y[fy]--;
vis[fx][fy] = 1;
dfs(fx, fy, step + 1); //走下一步
vis[fx][fy] = 0; //回溯
x[fx]++, y[fy]++;
}
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> y[i];
for (int i = 0; i < n; i++) cin >> x[i];
x[0]--, y[0]--; //起点的靶子提前减去一箭
//vis[0][0] = 1;
cout << 0 << " ";
dfs(0, 0, 0);
return 0;
}