每日刷题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;
}
相关推荐
朝朝又沐沐2 小时前
算法竞赛阶段二-数据结构(36)数据结构双向链表模拟实现
开发语言·数据结构·c++·算法·链表
薰衣草23333 小时前
一天两道力扣(6)
算法·leetcode
逝雪Yuki3 小时前
Leetcode——287. 寻找重复数
c++·leetcode·二分查找·双指针·环形链表
剪一朵云爱着3 小时前
力扣946. 验证栈序列
算法·
遇见尚硅谷3 小时前
C语言:*p++与p++有何区别
c语言·开发语言·笔记·学习·算法
天天开心(∩_∩)4 小时前
代码随想录算法训练营第三十二天
算法
YouQian7724 小时前
(AC)缓存系统
算法·缓存
艾莉丝努力练剑4 小时前
【数据结构与算法】数据结构初阶:详解排序(二)——交换排序中的快速排序
c语言·开发语言·数据结构·学习·算法·链表·排序算法
科大饭桶4 小时前
数据结构自学Day13 -- 快速排序--“前后指针法”
数据结构·算法·leetcode·排序算法·c
李永奉4 小时前
C语言-流程控制语句:for循环语句、while和do…while循环语句;
c语言·开发语言·c++·算法