每日刷题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;
}
相关推荐
姬公子5212 分钟前
leetcode hot100刷题日记——29.合并两个有序链表
c++·leetcode·链表
我不是小upper15 分钟前
详细到用手撕transformer下半部分
算法·机器学习·transformer
菜一头包34 分钟前
CPP中CAS std::chrono 信号量与Any类的手动实现
开发语言·c++
coding者在努力35 分钟前
高级数据结构与算法期末考试速成记录
数据结构·算法·动态规划·分治·速成·期末考试
new出对象41 分钟前
C++ 中的函数包装:std::bind()、std::function<>、函数指针与 Lambda
开发语言·c++·算法
宁静祥和----------1 小时前
编码总结如下
c++
NoneCoder1 小时前
Symbol、Set 与 Map:新数据结构探秘
前端·javascript·数据结构
理论最高的吻2 小时前
面试题 08.08. 有重复字符串的排列组合【 力扣(LeetCode) 】
c++·算法·leetcode·深度优先·回溯法
whoarethenext2 小时前
c/c++的opencv霍夫变换
c语言·c++·opencv
闪电麦坤953 小时前
数据结构:导论
数据结构