每日刷题c++

快速幂

cpp 复制代码
#include <iostream>
using namespace std;
#define int long long
int power(int a, int b, int p)
{
    int ans = 1;
    while (b)
    {
        if (b % 2)
        {
            ans *= a;
            ans %= p; // 随时取模
        }
        a *= a;
        a %= p; // 随时取模
        b /= 2;
    }
    return ans;
}
signed main()
{
    int a, b, p;
    cin >> a >> b >> p;
    int res = power(a, b, p);
    res %= p;
    cout << a << "^" << b << " mod " << p << "=" << res;
    return 0;
}

Shortest path of the king

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
int res;
string s[1000];
string a, b;
int x, y;
int main()
{
    cin >> a >> b;
    x = (a[0] - 'a') - (b[0] - 'a');
    y = (a[1] - '0') - (b[1] - '0');
    while (x > 0 && y > 0)
    {
        s[res] = "LD";
        res++;
        x--;
        y--;
    }
    while (x < 0 && y < 0)
    {
        s[res] = "RU";
        res++;
        x++;
        y++;
    }
    while (x > 0 && y < 0)
    {
        s[res] = "LU";
        res++;
        x--;
        y++;
    }
    while (x < 0 && y > 0)
    {
        s[res] = "RD";
        res++;
        x++;
        y--;
    }
    while (x > 0)
    {
        s[res] = "L";
        res++;
        x--;
    }
    while (x < 0)
    {
        s[res] = "R";
        res++;
        x++;
    }
    while (y > 0)
    {
        s[res] = "D";
        res++;
        y--;
    }
    while (y < 0)
    {
        s[res] = "U";
        res++;
        y++;
    }
    cout << res << endl;
    for (int i = 0; i < res; i++)
    {
        cout << s[i] << endl;
    }
    return 0;
}

It's Time To Duel

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    bool flag = false;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if (a[i] == 0)
        {
            flag = true;
        }
    }
    if (!flag)
    {
        cout << "YES" << endl;
        return;
    }
    for (int i = 1; i < n; i++)
    {
        if (a[i] == a[i + 1] && a[i] == 0)
        {
            cout << "YES" << endl;
            return;
        }
    }
    cout << "NO" << endl;
    return;
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
相关推荐
多打代码5 分钟前
2025.09.05 用队列实现栈 & 有效的括号 & 删除字符串中的所有相邻重复项
python·算法
mit6.8241 小时前
并查集|栈
c++
中国胖子风清扬2 小时前
Rust 序列化技术全解析:从基础到实战
开发语言·c++·spring boot·vscode·后端·中间件·rust
j_xxx404_2 小时前
数据结构:栈和队列力扣算法题
c语言·数据结构·算法·leetcode·链表
南莺莺2 小时前
假设一个算术表达式中包含圆括号、方括号和花括号3种类型的括号,编写一个算法来判别,表达式中的括号是否配对,以字符“\0“作为算术表达式的结束符
c语言·数据结构·算法·
THMAIL2 小时前
深度学习从入门到精通 - 神经网络核心原理:从生物神经元到数学模型蜕变
人工智能·python·深度学习·神经网络·算法·机器学习·逻辑回归
野犬寒鸦2 小时前
力扣hot100:旋转图像(48)(详细图解以及核心思路剖析)
java·数据结构·后端·算法·leetcode
墨染点香2 小时前
LeetCode 刷题【61. 旋转链表】
算法·leetcode·职场和发展
岁忧2 小时前
(LeetCode 面试经典 150 题) 200. 岛屿数量(深度优先搜索dfs || 广度优先搜索bfs)
java·c++·leetcode·面试·go·深度优先
一枝小雨3 小时前
【OJ】C++ vector类OJ题
数据结构·c++·算法·leetcode·oj题