头像

星瑞




离线:11小时前


活动打卡代码 AcWing 1381. 阶乘

星瑞
11小时前

纸上推算一下发现不能单取个位,还需要更多位,理论上需要%1000000但是%1000也能过,既然能过,那就这样吧~

#include<iostream>
using namespace std;
int n;
int res = 1;
signed main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        res*=i;
        while(res%10 == 0)
            res/=10;
        res%=1000;
    }
    cout<<res%10<<endl;
}


活动打卡代码 AcWing 1371. 货币系统

星瑞
12小时前
#include<bits/stdc++.h>
using namespace std;
int n,m;
long long dp[100005];
int a[30];
int cnt;
signed main()
{
    cin>>n>>m;
    dp[0] = 1;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        for(int j=a[i];j<=m;j++)
        {
            dp[j]+=dp[j-a[i]];
        }
    }
    cout<<dp[m]<<endl;
}


活动打卡代码 AcWing 1432. 棋盘挑战

星瑞
12小时前
#include<bits/stdc++.h>
using namespace std;
const int maxn = 15;
int res[maxn];
int n;
int col[maxn],dg[maxn*2],udg[maxn*2];
int ans;

void dfs(int x)
{
    if(x>n)
    {
        ans++;
        if(ans<=3)
        {
            for(int i=1;i<=n;i++)
            {
                cout<<res[i]<<" ";
            }
            cout<<endl;
        }
        return;
    }
    for(int i=1;i<=n;i++)
    {
        if(!col[i]&&!dg[x+i]&&!udg[x-i+n])
        {
            res[x] = i;
            dg[x+i] = udg[x-i+n] = col[i] = 1;
            dfs(x+1);
            dg[x+i] = udg[x-i+n] = col[i] = 0;
            res[x] = 0;
        }
    }
}
signed main(){
    cin>>n;
    dfs(1);
    cout<<ans<<endl;
    return 0;
}


活动打卡代码 AcWing 1341. 十三号星期五

星瑞
3天前
#include <iostream>
using namespace std;
int week(int y, int m, int d)
{
    if (m == 1 || m == 2)
        m = m + 12, y--;
    return (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7;//公式
}
int main()
{
    int n = 0;
    cin >> n;
    int weekday[7] = {0};//存储星期的天数
    for (int y = 1900; y < 1900 + n; y++)
        for (int m = 1; m <= 12; m++)
        {
            int w = week(y, m, 13);//公式计算每一年,每一月的13号是星期几
            weekday[w]++;//对应星期天数加 1
        }

    for(int i = 6; i < 6 + 7; i++)
        cout << weekday[i % 7] << " ";
    return 0;
}




活动打卡代码 AcWing 754. 平方矩阵 II

星瑞
3天前

观察数组易得

#include<bits/stdc++.h>
using namespace std;
int n;
signed main() {
    while (cin >> n && n)
    {
        for(int i=1;i<=n;i++)
            for (int j = 1; j <= n; j++)
            {
                if (i == j)
                    cout << 1 << " ";
                else
                    cout << abs(i - j) + 1 << " ";
                if (j == n)
                    cout << endl;
            }
        cout << endl;
    }
}


活动打卡代码 AcWing 1532. 找硬币

星瑞
5天前

排序+双指针

#include<bits/stdc++.h>
using namespace std;
int n, m;
int a[100005];
int p, q;
signed main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)cin >> a[i];
    sort(a + 1, a + 1 + n);
    p = 1; q = n;
    int ok = 0;
    while (p < q)
    {
        if (a[p] + a[q] > m) q--;
        else if (a[p] + a[q] < m) p++;
        else { ok = 1;break; }
    }
    if (ok)
        cout << a[p] << " " << a[q] << endl;
    else
        puts("No Solution");
}


活动打卡代码 AcWing 1208. 翻硬币

星瑞
6天前

少了模拟过程的更简便的写法
因为有解,所以必定两个为一组
判断不同的字符之间的距离,累加即可
无需模拟翻转

#include<bits/stdc++.h>

using namespace std;


signed main()
{
    string ss, cc;
    cin >> ss >> cc;
    int len = ss.size();
    int f = -1;
    int ans = 0;
    for (int i = 0; i < len; i++)
    {
        if (f == -1 && ss[i] != cc[i])
        {
            f = i;
        }
        else if (ss[i] != cc[i])
        {
            ans += i - f;
            f = -1;
        }
    }
    cout << ans << endl;
    return 0;
}


活动打卡代码 AcWing 1346. 回文平方

星瑞
6天前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

char get(int x)//将x转换为字符形式
{
    if (x <= 9) return x + '0';
    return x - 10 + 'A';
}

string base(int n, int b)//将n转换为b进制,返回对应字符串
{
    string res;
    while (n)
    {
        res += get(n % b);
        n /= b;
    }
    reverse(res.begin(), res.end());//翻转回高位在左
    return res;
}

bool check(string s)//检查s是否是回文
{
    for (int i = 0, j = s.size() - 1; i < j; i ++, j -- )
        if (s[i] != s[j])
            return false;
    return true;
}

int main()
{
    int b;
    cin >> b;

    for (int i = 1; i <= 300; i ++ )//暴力
    {
        string num = base(i * i, b);
        if (check(num))
            cout << base(i, b) << ' ' << num << endl;
    }

    return 0;
}


活动打卡代码 AcWing 429. 奖学金

星瑞
6天前

重载运算符实现结构体排序

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int a;
    int b;
    int c;
    int sum;
    int no;
    bool operator <(const node &x)const
    {
        if(x.sum != sum)
            return x.sum<sum;
        if(x.a != a)
            return x.a<a;
        return x.no>no;
    }
}p[305];
int n;
int a,b,c;
signed main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a>>b>>c;
        p[i].a = a;
        p[i].b = b;
        p[i].c = c;
        p[i].sum = a+b+c;
        p[i].no = i;
    }
    sort(p+1,p+1+n);
    for(int i=1;i<=5;i++)
    {
        cout<<p[i].no<<" "<<p[i].sum<<endl;
    }
    return 0;
}


活动打卡代码 AcWing 422. 校门外的树

星瑞
8天前

数据不大,暴力就好,如果数据大的话可以用sort+pair降低复杂度至O(nlogn)

#include<iostream>
using namespace std;

int n, m;
int a[10005];
int x, y;
int ans;
signed main()
{
    cin >> n >> m;
    //ans = n;
    for (int i = 0; i < m; i++)
    {
        cin >> x >> y;
        for (int j = x; j <= y; j++)
        {
            if(!a[j])
                ans++;
            a[j] = 1;
        }
    }
    cout << n-ans+1 << endl;
    return 0;
}