每日刷题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;
}
相关推荐
会开花的二叉树几秒前
吃透Reactor多线程:EventLoop_Channel_ThreadPool协作原理
开发语言·c++·tcp/ip·servlet
Jm_洋洋2 分钟前
【C++进阶】虚函数、虚表与虚指针:多态底层机制剖析
java·开发语言·c++
近津薪荼7 分钟前
优选算法——滑动窗口1(单调性)
c++·学习·算法
头发还没掉光光7 分钟前
Linux 高级 IO 深度解析:从 IO 本质到 epoll全面讲解
linux·服务器·c语言·c++
爱装代码的小瓶子8 分钟前
【C++与Linux基础】进程如何打开磁盘文件:从open()到文件描述符的奇妙旅程(更多源码讲解)
linux·开发语言·c++
diediedei8 分钟前
嵌入式C++驱动开发
开发语言·c++·算法
80530单词突击赢11 分钟前
C++容器对比:map与unordered_map全解析
c++
燃于AC之乐13 分钟前
《算法实战笔记》第10期:六大算法实战——枚举、贪心、并查集、Kruskal、双指针、区间DP
算法·贪心算法·图论·双指针·区间dp·二进制枚举
田野追逐星光13 分钟前
STL中容器list -- 讲解超详细
开发语言·c++·list
diediedei17 分钟前
高性能计算通信库
开发语言·c++·算法