T1 100% 一个循环数组 如果直接就是递增的就不用处理 否则需要找个递减点先把他按顺序弄回去 然后计算起始结束下标要把移动的下标计算进去
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, target;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cin >> target;
int change = 0;
for (int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
change = i + 1;
vector<int> tmp(n);
for (int j = 0; j < n; j++) {
tmp[j] = a[(j + change) % n];
}
a = tmp;
break;
}
}
int start = INT_MAX;
int end = INT_MIN;
for (int i = 0; i < n; i++) {
if (a[i] == target) {
start = min(start, i);
end = max(end, i);
}
}
cout << (start + change) % n << " " << (end + change) % n << endl;
return 0;
}
T2 100% 将相邻的1连起来,然后利用多源bfs分层,首先将最左侧是1压入队列中,跑到最右侧,层数最小的就是最短的路径
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
bool ifon(int x, int y, int n, int m)
{
return x >= 0 && x < n&& y >= 0 && y < m;
}
int main()
{
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n, vector<int>(m, 0));
auto vis = grid;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> grid[i][j];
queue<pair<int,int>> q;
queue<int> sq;
for (int i = 0; i < n; i++) {
if (grid[i][0] == 1) {
q.push({ i,0 });
vis[i][0] = 1;
sq.push(0);
}
}
int ans = 1e9;
int dx[4] = {-1,1,0,0};
int dy[4] = { 0,0,-1,1 };
while (q.size())
{
int x = q.front().first, y = q.front().second;
int stp = sq.front();
q.pop();
sq.pop();
if (y == m - 1)
{
ans = min(ans, stp);
continue;
}
for (int i = 0; i < 4; i++)
{
int tox = x + dx[i], toy = y + dy[i];
if (ifon(tox, toy, n, m) == 0)
continue;
if (vis[tox][toy]||grid[tox][toy]==0)continue;
vis[tox][toy] = 1;
q.push({ tox,toy });
sq.push(stp + 1);
}
}
if (ans == 1e9)
cout << -1;
else
cout << ans;
}
T3 65% 大模拟,用map<string,int> mp存储每个变量的值,然后利用字符串切割处理字符串,模拟计算,注意溢出用long long
#include<iostream>
#include<vector>
#include<string>
#include<map>
using namespace std;
long long cau(long long a, char c, long long b)
{
if (c == '+') return a + b;
else if (c == '-') return a - b;
else if (c == '*') return a * b;
else return a / b;
}
int main()
{
map<string, long long> mp;
string op;
cin >> op;
while (1)
{
if (op == "let")
{
string name;
cin >> name;
int right = 1;
if (name[0] >= '0' && name[0] <= '9')
{
right = 0;
}
for (char c : name)
{
if (!(c == '_' || (c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
right = 0;
}
string next;
cin >> next;
if (next != "=")
right = 0;
if (right == 0)
{
cout << "<syntax-error>" << endl;
return 0;
}
string pre;
cin >> pre;
if (pre[0] == '-' || (pre[0] >= '0' && pre[0] <= '9'))
mp[name] = stoll(pre.c_str());
else
mp[name] = mp[pre];
string pa;
while (cin >> pa)
{
if (pa == "let" || (pa.size() >= 3 && pa.substr(0, 3) == "out"))
break;
if (pa[0] == '+' || pa[0] == '-' || pa[0] == '*' || pa[0] == '/') {
pre = pa;
continue;
}
if (pa[0] == '-' || (pa[0] >= '0' && pa[0] <= '9'))
mp[name] = cau(mp[name], pre[0], stoll(pa.c_str()));
else
mp[name] = cau(mp[name], pre[0], mp[pa]);
}
op = pa;
}
else if (op.size() >= 3 && op.substr(0, 3) == "out")
{
int n = op.size();
string pa = op.substr(4, n - 5);
long long pt = 0;
if (pa[0] == '-' || (pa[0] >= '0' && pa[0] <= '9'))
pt = stoll(pa.c_str());
else if (mp.find(pa) != mp.end())
pt = mp[pa];
if (pt < -2147483648)
cout << "<underflow>" << endl;
else if (pt > 2147483647)
cout << "<overflow>" << endl;
else if (mp.find(pa) == mp.end())
cout << "<undefined>" << endl;
else
cout << pt << endl;
op = "over";
cin >> op;
}
else
break;
}
}
#华为##笔试#