第一题
丢失报文的位置
思路:从数组最小索引开始遍历
#include <iostream>
#include <vector>
using namespace std;
// 求最小索引值
int getMinIdx(vector<int> &arr)
{int minidx = 0;for (int i = 0; i < arr.size(); i++){if (arr[i] < arr[minidx]){minidx = i;}}return minidx;
}
int main()
{int n;cin >> n;vector<int> nums(n);for (int i = 0; i < n; i++){cin >> nums[i];}int sn;cin >> sn;int p = getMinIdx(nums);cout << p << endl;// 初始化左右边界int l = -1, r = -1;for (int i = 0; i < n; i++){// 模n取位置索引int t = (p + i) % n;// 比较snif (nums[t] == sn){// 更新一次左边界if (l == -1){l = t;}// 不断更新右边界r = t;}}// 输出cout << l << " " << r;return 0;
}
第二题
快速传球
思路:bfs+队列
#include <iostream>
#include <vector>
#include <queue>
using namespace std;int main()
{// bfsint m, n;cin >> m >> n;vector<vector<int>> q = vector<vector<int>>(m, vector<int>(n));// 记录数组每个节点最短距离// 初始化为-1vector<vector<int>> d = vector<vector<int>>(m, vector<int>(n, -1));for (int i = 0; i < m; i++){for (int j = 0; j < n; j++){cin >> q[i][j];}}// 方向数组int pos[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};// 队列遍历每个点queue<pair<int, int>> que;// 第一列男同学入队for (int i = 0; i < m; i++){if (q[i][0] == 1){que.push(pair<int, int>(i, 0));// 路径数组更新d[i][0] = 0;}}// 遍历队列while (!que.empty()){// 出队 取点pair<int, int> t = que.front();que.pop();// 取坐标int x = t.first;int y = t.second;// 遍历四个方向 取下一个坐标for (int i = 0; i < 4; i++){int nx = x + pos[i][0];int ny = y + pos[i][1];// 过滤下一个点if (nx < 0 || ny < 1 || nx > m - 1 || ny > n - 1 || d[nx][ny] != -1 || q[nx][ny] == 0)continue;// 更新节点距离d[nx][ny] = d[x][y] + 1;// 继续入队que.push(pair<int, int>(nx, ny));}}// 初始化最短路径int mind = INT32_MAX;// 遍历每行 求最后一列最短距离for (int i = 0; i < m; i++){// 如果右下角是男同学 可以传到if (q[i][n - 1] == 1){mind = min(mind, d[i][n - 1]);}}// 判断是否有最短距离if (mind == INT32_MAX)cout << -1;// 返回最短路径距离cout << mind;return 0;
}
第三题
简易计算器
思路:真大模拟,代码量巨大,还是没有写全,AC不了,而且c++我不会处理输入输出,用了Golang。。。
package mainimport ("bufio""fmt""os""regexp""strconv""strings"
)type inp struct {v stringname stringequal stringexpress []string
}const (SYNTAX_ERROR = "<syntax-error>" // 语法错误UNDEFINED = "<undefined>" // 未定义UNDERFLOW = "<underflow>" // 下溢OVERFLOW = "<overflow>" // 溢出
)// 未定义错误
func isUndeined(m map[string]*inp, name string) bool {if _, ok := m[name]; ok {return false}return true
}// 是否有语法错误
func isSynErr(ex []string, name string) bool {// 表达式错误if len(ex)%2 == 0 {return true}// 正则match, _ := regexp.MatchString("^[a-zA-Z]", name)// _开头或者字母开头if strings.HasPrefix(name, "_") || match {return false}return true
}// 输出结果
func getRes(m map[string]*inp, ex []string) any {e1, err := strconv.Atoi(ex[0])if err != nil {st := m[ex[0]]e1, _ = strconv.Atoi(st.express[0])}// 初始化结果var ret int = e1// 标记符号 用于计算var sign stringfor k, v := range ex {if k > 0 {if v == "+" || v == "-" || v == "*" || v == "/" {sign = vcontinue}// 从左到右一次计算if sign == "+" {e, err := strconv.Atoi(v)if err != nil {st := m[v]e, _ = strconv.Atoi(st.express[0])}ret += e} else if sign == "-" {e, err := strconv.Atoi(v)if err != nil {st := m[v]e, _ = strconv.Atoi(st.express[0])}ret -= e} else if sign == "*" {e, err := strconv.Atoi(v)if err != nil {st := m[v]e, _ = strconv.Atoi(st.express[0])}ret *= e} else if sign == "/" {e, err := strconv.Atoi(v)if err != nil {st := m[v]e, _ = strconv.Atoi(st.express[0])}ret /= e}}}// 是否上溢if ret > 2147483647 {return OVERFLOW}// 是否下溢if ret < -2147483647 {return UNDERFLOW}// 返回return ret
}// 语法错误
func main() {// Golang输入// fmt.Scan(&v, &name, &equal, &num)// map表记录m := make(map[string]*inp, 24)n := 0for {// var (// v string// name string// equal string// express []string// )// // 输入操作 换行结束// fmt.Scanln(&v, &name, &equal, &express)n++scan := bufio.NewReader(os.Stdin)// 读取一行数据line, _, _ := scan.ReadLine()// fmt.Println(string(line))data := string(line)lis := strings.Split(data, " ")var recv [30]stringfor k, v := range lis {recv[k] = v}na := recv[1]val := recv[0]eq := recv[2]var ex []stringfor _, v := range recv[3:] {if v != "" {ex = append(ex, v)}}m[recv[1]] = &inp{v: val,name: na,equal: eq,express: ex,}// 前缀是否以out开头if strings.HasPrefix(val, "out") {// 截取左边索引l := strings.Index(val, "(")// 截取值va := val[l+1 : len(val)-1]st := m[va]// fmt.Println(val)// 测试// fmt.Printf("%+v\n", m[val])// out := m[val]if isUndeined(m, va) {fmt.Println(UNDEFINED)} else if isSynErr(st.express, st.name) {fmt.Println(SYNTAX_ERROR)} else {fmt.Println(getRes(m, st.express))}}// 控制最多24行if n > 24 {break}}
}